Dear All, I am given 15 different data sets and I would like to generate a panel showing all of them. Each dataset will be presented either as a boxplot or as a histogram. There are several possible ways to achieve this (as far as I know) (1) using plot and mfrow() (2) using lattice (3) using ggplot/ggplot2 I am not very experienced (to be euphemistic) about (2) and (3). My question then is: how would you try to organize these 15 histograms/boxplots into a single figure? Can anyone provide me with a simple example (with artificial data) for (2) and (3) (or point me to some targeted online resource)? Any suggestion is welcome. Many thanks Lorenzo
Hi, Here is some artificial data followed by minimal ggplot2 and lattice examples, makeUpData <- function(){ data.frame(x=sample(letters[1:4], 100, repl=TRUE), y=rnorm(100)) } datasets <- replicate(15, makeUpData(), simplify=FALSE) names(datasets) <- paste("dataset", seq_along(datasets), sep="") str(datasets) require(reshape) ## combine the datasets in one long format data.frame m <- melt(datasets, meas=c("y")) str(m) require(ggplot2) ggplot(m)+ geom_boxplot(mapping=aes(x, value))+ facet_wrap(~L1) # or more concisely qplot(x, value, data=m, geom="boxplot", facets=~L1) require(lattice) bwplot(value~x | L1, data=m) HTH, baptiste 2009/12/29 Lorenzo Isella <lorenzo.isella at gmail.com>:> Dear All, > I am given 15 different data sets and I would like to generate a panel > showing all of them. > Each dataset will be presented either as a boxplot or as a histogram. > There are several possible ways to achieve this (as far as I know) > > (1) using plot and mfrow() > (2) using lattice > (3) using ggplot/ggplot2 > > I am not very experienced (to be euphemistic) about (2) and (3). > My question then is: how would you try to organize these 15 > histograms/boxplots ?into a single figure? > Can anyone provide me with a simple example (with artificial data) for > (2) and (3) (or point me to some targeted online resource)? > Any suggestion is welcome. > Many thanks > > Lorenzo > > ______________________________________________ > 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. >
I forgot the base graphics way, ## divide the window in 4x4 cells par(mfrow=n2mfrow(length(datasets))) ## loop over the list of datasets and plot each one be.quiet <- lapply(datasets, function(ii) boxplot(y~x, data=ii)) ggplot2 has a website with many examples, http://had.co.nz/ggplot2/ as well as a book. Lattice also has a dedicated book, and a companion website with the figures, http://r-forge.r-project.org/projects/lmdvr/ HTH, baptiste 2009/12/29 baptiste auguie <baptiste.auguie at googlemail.com>:> Hi, > > Here is some artificial data followed by minimal ggplot2 and lattice examples, > > makeUpData <- function(){ > ?data.frame(x=sample(letters[1:4], 100, repl=TRUE), y=rnorm(100)) > ?} > > datasets <- replicate(15, makeUpData(), simplify=FALSE) > names(datasets) <- paste("dataset", seq_along(datasets), sep="") > > str(datasets) > > require(reshape) > ## combine the datasets in one long format data.frame > m <- melt(datasets, meas=c("y")) > > str(m) > > require(ggplot2) > > ggplot(m)+ > ?geom_boxplot(mapping=aes(x, value))+ > ?facet_wrap(~L1) > > # or more concisely > qplot(x, value, data=m, geom="boxplot", facets=~L1) > > require(lattice) > > bwplot(value~x | L1, data=m) > > HTH, > > baptiste > > 2009/12/29 Lorenzo Isella <lorenzo.isella at gmail.com>: >> Dear All, >> I am given 15 different data sets and I would like to generate a panel >> showing all of them. >> Each dataset will be presented either as a boxplot or as a histogram. >> There are several possible ways to achieve this (as far as I know) >> >> (1) using plot and mfrow() >> (2) using lattice >> (3) using ggplot/ggplot2 >> >> I am not very experienced (to be euphemistic) about (2) and (3). >> My question then is: how would you try to organize these 15 >> histograms/boxplots ?into a single figure? >> Can anyone provide me with a simple example (with artificial data) for >> (2) and (3) (or point me to some targeted online resource)? >> Any suggestion is welcome. >> Many thanks >> >> Lorenzo >> >> ______________________________________________ >> 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. >> >
Dear Baptiste, Thanks a lot for the excellent example, which convinced me to start studying ggplot2. A trivial question: is there an easy way to generate a boxplot without outliers? Using R standard plotting facilities, this amounts to giving outline=FALSE within boxplot. Can I easily achieve the same using ggplot2? Beside not plotting the outliers, I also would like the y range to adjust automatically. I did some online research, someone suggested preprocessing the data, but I can hardly believe that this is the only way to go. Cheers Lorenzo baptiste auguie wrote:> I forgot the base graphics way, > > ## divide the window in 4x4 cells > par(mfrow=n2mfrow(length(datasets))) > > ## loop over the list of datasets and plot each one > be.quiet <- lapply(datasets, function(ii) boxplot(y~x, data=ii)) > > > ggplot2 has a website with many examples, > http://had.co.nz/ggplot2/ > as well as a book. > > Lattice also has a dedicated book, and a companion website with the figures, > http://r-forge.r-project.org/projects/lmdvr/ > > HTH, > > baptiste > > 2009/12/29 baptiste auguie <baptiste.auguie at googlemail.com>: > >> Hi, >> >> Here is some artificial data followed by minimal ggplot2 and lattice examples, >> >> makeUpData <- function(){ >> data.frame(x=sample(letters[1:4], 100, repl=TRUE), y=rnorm(100)) >> } >> >> datasets <- replicate(15, makeUpData(), simplify=FALSE) >> names(datasets) <- paste("dataset", seq_along(datasets), sep="") >> >> str(datasets) >> >> require(reshape) >> ## combine the datasets in one long format data.frame >> m <- melt(datasets, meas=c("y")) >> >> str(m) >> >> require(ggplot2) >> >> ggplot(m)+ >> geom_boxplot(mapping=aes(x, value))+ >> facet_wrap(~L1) >> >> # or more concisely >> qplot(x, value, data=m, geom="boxplot", facets=~L1) >> >> require(lattice) >> >> bwplot(value~x | L1, data=m) >> >> HTH, >> >> baptiste >> >> 2009/12/29 Lorenzo Isella <lorenzo.isella at gmail.com>: >> >>> Dear All, >>> I am given 15 different data sets and I would like to generate a panel >>> showing all of them. >>> Each dataset will be presented either as a boxplot or as a histogram. >>> There are several possible ways to achieve this (as far as I know) >>> >>> (1) using plot and mfrow() >>> (2) using lattice >>> (3) using ggplot/ggplot2 >>> >>> I am not very experienced (to be euphemistic) about (2) and (3). >>> My question then is: how would you try to organize these 15 >>> histograms/boxplots into a single figure? >>> Can anyone provide me with a simple example (with artificial data) for >>> (2) and (3) (or point me to some targeted online resource)? >>> Any suggestion is welcome. >>> Many thanks >>> >>> Lorenzo >>> >>> ______________________________________________ >>> 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. >>> >>>