Skip to content

Newbie question - struggling with boxplots

5 messages · Geoffrey Stoel, Ista Zahn, Nordlund, Dan (DSHS/RDA) +2 more

#
On Tue, Aug 16, 2011 at 5:24 PM, Geoffrey Stoel <g.stoel at hourglazz.com> wrote:
You could use ggplot, like this:

library(ggplot2)
iris.m <- melt(iris, id = "Species")
ggplot(iris.m, aes(x = variable, y = value)) + geom_boxplot() +
facet_wrap(~Species, nrow = 1)

best,
Ista

  
    
#
Nice plot.  I know that this is pretty much what the OP asked for, but I find the arrangement below a little easier to make sense of.

ggplot(iris.m, aes(x = Species, y = value)) + geom_boxplot() +
   facet_wrap(~variable, nrow = 1)


Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204
#
On Aug 16, 2011, at 5:24 PM, Geoffrey Stoel wrote:

            
require(lattice)
require(reshape)
iris.m <- melt(iris, id = "Species")
 > str(iris.m)
'data.frame':	600 obs. of  3 variables:
  $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1  
1 1 1 1 ...
  $ variable: Factor w/ 4 levels "Sepal.Length",..: 1 1 1 1 1 1 1 1 1  
1 ...
  $ value   : num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...

 > bwplot(value ~ variable | Species, iris.m       )

There would also be a way to send a list of Species to a function and  
do it in base graphics but that is often less economical. In this  
can,  not so bad:

 > opar <- par(mfcol=c(2,2))
 > sapply(unique(iris$Species), function(x)  
boxplot( subset(iris,Species==x, select = (1:4))) )
 > par(opar)
David Winsemius, MD
West Hartford, CT
#
something like this?

 par(mfrow=c(2,2))
 boxplot(Sepal.Length~Species,data=iris,main='Sepal Length')
 boxplot(Sepal.Width~Species,data=iris,main='Sepal Width')
 boxplot(Petal.Length~Species,data=iris,main='Petal Length')
 boxplot(Petal.Width~Species,data=iris,main='Petal Width')
 par(mfrow=c(1,1))
On Wed, Aug 17, 2011 at 7:24 AM, Geoffrey Stoel <g.stoel at hourglazz.com> wrote: