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... If you want to help me out... would be deeply appreciated.. Kind regards, Geoffrey (new to R and datamining) [[alternative HTML version deleted]]
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) > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org
On Aug 16, 2011, at 5:24 PM, Geoffrey Stoel 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...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)> > If you want to help me out... would be deeply appreciated.. > > Kind regards, > > Geoffrey > > (new to R and datamining) > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.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:> 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... > > If you want to help me out... would be deeply appreciated.. > > Kind regards, > > Geoffrey > > (new to R and datamining) > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >