[R] ggplot2 - boxplot of variables / columns
Hi, ggplot/qplot is great - it has really helped me do some nice things. However, simple boxplot of different columns/variables is a bit tricky, because of (i think) qplot's generic Y conditional on X input form. Se below. # Some data: a - rnorm(100) b - rnorm(100,1,2) c - rnorm(100,2,0.5) # normal boxplot of a,b,c boxplot(a,b,c) # Looks good library(ggplot2) # loads qqplot2 # Tries do replicate the simple boxplot qplot(a,b,c, geom=boxplot) # Not good # Workaround d - c(a,b,c) e - c(rep(a,100),rep(b,100),rep(c,100)) qplot(e,d,geom=boxplot) # Works - but there must be a simpler way? What is the simple to compare multiple variables like this? thanks in advance __ R-help@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.
Re: [R] ggplot2 - boxplot of variables / columns
On Apr 21, 2009, at 10:42 AM, Andreas Christoffersen wrote: Hi, ggplot/qplot is great - it has really helped me do some nice things. However, simple boxplot of different columns/variables is a bit tricky, because of (i think) qplot's generic Y conditional on X input form. Se below. # Some data: a - rnorm(100) b - rnorm(100,1,2) c - rnorm(100,2,0.5) # normal boxplot of a,b,c boxplot(a,b,c) # Looks good library(ggplot2) # loads qqplot2 # Tries do replicate the simple boxplot qplot(a,b,c, geom=boxplot) # Not good # Workaround d - c(a,b,c) e - c(rep(a,100),rep(b,100),rep(c,100)) qplot(e,d,geom=boxplot) # Works - but there must be a simpler way? qplot(ind, values, data=stack(data.frame(a,b,c)), geom=boxplot) I first tried stack(list(a,b,c)) but did not get the expected results. If anyone wants to enlighten me on why, I would be happy to offer a rewrite of the stack help page that clarifies my inability to parse it correctly in its current incarnation. What is the simple to compare multiple variables like this? David Winsemius, MD Heritage Laboratories West Hartford, CT __ R-help@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.
Re: [R] ggplot2 - boxplot of variables / columns
Dear Andreas, melt() and cast() are nice tools for this kind of problems. They both reside in the reshape package that automatic loaded when ggplot2 is. a - rnorm(100) b - rnorm(100,1,2) c - rnorm(100,2,0.5) ds - data.frame(a = a, b = b, c = c) library(ggplot2) # loads qqplot2 ggplot(melt(ds), aes(x = variable, y = value)) + geom_boxplot() HTH, Thierry ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 thierry.onkel...@inbo.be www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey -Oorspronkelijk bericht- Van: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] Namens Andreas Christoffersen Verzonden: dinsdag 21 april 2009 16:42 Aan: r-help@r-project.org Onderwerp: [R] ggplot2 - boxplot of variables / columns Hi, ggplot/qplot is great - it has really helped me do some nice things. However, simple boxplot of different columns/variables is a bit tricky, because of (i think) qplot's generic Y conditional on X input form. Se below. # Some data: a - rnorm(100) b - rnorm(100,1,2) c - rnorm(100,2,0.5) # normal boxplot of a,b,c boxplot(a,b,c) # Looks good library(ggplot2) # loads qqplot2 # Tries do replicate the simple boxplot qplot(a,b,c, geom=boxplot) # Not good # Workaround d - c(a,b,c) e - c(rep(a,100),rep(b,100),rep(c,100)) qplot(e,d,geom=boxplot) # Works - but there must be a simpler way? What is the simple to compare multiple variables like this? thanks in advance __ R-help@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. Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document. The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document. __ R-help@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.
Re: [R] ggplot2 - boxplot of variables / columns
David, you solution qplot(ind, values, data=stack(data.frame(a,b,c)), geom=boxplot) Works a treat - thank you! Thierry, your solution ds - data.frame(a = a, b = b, c = c) library(ggplot2) # loads qqplot2 ggplot(melt(ds), aes(x = variable, y = value)) + geom_boxplot() Also works. I can even combine the two solutions, using stack / melt interchangeably. I am very glad that today I both learned melt/stack and Thierry has made me curious as to the finer ggplots fine tuning wizardry in ggplot vs. the simple qplot. Again - thx. __ R-help@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.