[R] Newbie question - struggling with boxplots

Nordlund, Dan (DSHS/RDA) NordlDJ at dshs.wa.gov
Wed Aug 17 02:10:04 CEST 2011


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Ista Zahn
> Sent: Tuesday, August 16, 2011 4:13 PM
> To: g.stoel at hourglazz.com
> Cc: r-help at r-project.org
> Subject: Re: [R] Newbie question - struggling with boxplots
> 
> On Tue, Aug 16, 2011 at 5:24 PM, Geoffrey Stoel <g.stoel at hourglazz.com>
> wrote:
> > Hopefully I will not be flamed for this on the list, but I am
> starting out
> > with R and having some trouble with combining plots.
> >
> > I am playing with the famous iris dataset (checking out example
> dataset in R
> > while reading through Introduction to datamining)
> >
> > What I would like to do is create three graphs (combined boxplots)
> besides
> > each other for each of the three species (Setosa, Versicolour and
> Virginica)
> > with each graph showing showing four boxplots Sepal.Length,
> Sepal.Width,
> > Petal.Length and Petal.Width.
> >
> > I can create the boxplot for the total dataset by doing the
> following:
> >
> > data(iris);
> > boxplot(iris[1:4]);
> >
> > However I would like to have this repeated for each Species in
> iris$Species,
> > I know I can do this with:
> >
> > boxplot(subset(iris,Species=="setosa", select = (1:4)));
> > boxplot(subset(iris,Species=="versicolor", select = (1:4)));
> > boxplot(subset(iris,Species=="virginica", select = (1:4)));
> >
> > but I am lazy AND I want all three to be plotted besides each other
> AND I
> > don't want to manually type the Species myself.
> >
> > is there an easier way to do this.... probably yes...
> 
> 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
> >
> > If you want to help me out... would be deeply appreciated..
> >
> > Kind regards,
> >
> > Geoffrey
> >
> > (new to R and datamining)
> >

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




More information about the R-help mailing list