Hi, I was wondering if there is a ready made function or parameter for indicating the sample size in boxplots? Here's what I came up with so far: library(ISwR) data(energy) attach(energy) boxplot(expend~stature) sample.size <- tapply(expend, stature, length) sample.size <- paste("N=", sample.size, sep="") mtext(sample.size, at=1:length(unique(stature)), line=2, side=1) TIA, Patrick -- "What happens if a big asteroid hits Earth ? Judging from realistic simulations involving a sledge hammer and a common laboratory frog, we can assume it will be pretty bad." -- Dave Barry
On Wed, 2004-09-29 at 07:46, Patrick Drechsler wrote:> Hi, > > I was wondering if there is a ready made function or parameter > for indicating the sample size in boxplots? > > Here's what I came up with so far: > > library(ISwR) > data(energy) > attach(energy) > boxplot(expend~stature) > sample.size <- tapply(expend, stature, length) > sample.size <- paste("N=", sample.size, sep="") > mtext(sample.size, at=1:length(unique(stature)), line=2, side=1)Note that boxplot() returns values, which includes a variety of summary information on each group within your data. See the "Value" section of ?boxplot and ?boxplot.stats for more information. Thus, you can do something like (using the first example in ?boxplot): data(InsectSprays) # Save the returned values from boxplot() in "S" S <- boxplot(count ~ spray, data = InsectSprays, col = "lightgray") # S$n is the sample size for each group # S$names contains the group names mtext(side = 1, text = S$n, at = 1:length(S$names), line = 2) HTH, Marc Schwartz
On Wed, 29 Sep 2004, Patrick Drechsler wrote:> Hi, > > I was wondering if there is a ready made function or parameter > for indicating the sample size in boxplots? > > Here's what I came up with so far: > > library(ISwR) > data(energy) > attach(energy) > boxplot(expend~stature) > sample.size <- tapply(expend, stature, length) > sample.size <- paste("N=", sample.size, sep="") > mtext(sample.size, at=1:length(unique(stature)), line=2, side=1) >Perhaps use the names= argument (width can help too):> boxplot(expend~stature, width=sample.size/length(expend),+ names=paste(levels(stature), ", N=", sample.size, sep=""))> TIA, > > Patrick >-- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Breiviksveien 40, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93 e-mail: Roger.Bivand at nhh.no
Hi Marc, Marc Schwartz wrote on 29 Sep 2004 14:08:19 MET:> On Wed, 2004-09-29 at 07:46, Patrick Drechsler wrote: >> I was wondering if there is a ready made function or parameter >> for indicating the sample size in boxplots?[...]> Note that boxplot() returns values, which includes a variety of > summary information on each group within your data. See the > "Value" section of ?boxplot and ?boxplot.stats for more > information. > > Thus, you can do something like (using the first example in > ?boxplot): > > data(InsectSprays) > > # Save the returned values from boxplot() in "S" > S <- boxplot(count ~ spray, data = InsectSprays, col = "lightgray") > > # S$n is the sample size for each group > # S$names contains the group names > mtext(side = 1, text = S$n, at = 1:length(S$names), line = 2)Thank you very much--just what I was looking for! Patrick -- "Ludwig Boltzmann, who spent much of his life studying statistical mechanics, died in 1906, by his own hand. Paul Ehrenfest, carrying on the work, died similarly in 1933. Now it is our turn to study statistical mechanics. Perhaps it will be wise to approach the subject cautiously." from "States of Matter" by David Goodstein
Here is how I do it: function (x, ...) { bx <- boxplot(x, plot = F, ...) bx$names <- paste(bx$names, "\nn = ", bx$n, sep = "") tmp <- bxp(bx, tcl = 0, xaxt = "n") axis(1, at = tmp, labels = bx$names, tick = FALSE) invisible(c(bx, list(x = tmp))) } -Don At 2:46 PM +0200 9/29/04, Patrick Drechsler wrote:>Hi, > >I was wondering if there is a ready made function or parameter >for indicating the sample size in boxplots? > >Here's what I came up with so far: > >library(ISwR) >data(energy) >attach(energy) >boxplot(expend~stature) >sample.size <- tapply(expend, stature, length) >sample.size <- paste("N=", sample.size, sep="") >mtext(sample.size, at=1:length(unique(stature)), line=2, side=1) > >TIA, > >Patrick >-- >"What happens if a big asteroid hits Earth ? Judging from > realistic simulations involving a sledge hammer and a common > laboratory frog, we can assume it will be pretty bad." > -- Dave Barry > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! R-project.org/posting-guide.html-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA
Also note that boxplot.n in the gplots library (part of the gregmisc bundle) automatically adds the number of observations. -Greg> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of Patrick > Drechsler > Sent: Wednesday, September 29, 2004 1:47 PM > To: r-help at stat.math.ethz.ch > Subject: Re: [R] displaying sample size in boxplots > > > > Martin Maechler wrote on 29 Sep 2004 17:11:13 MET: > > >>>>>> "Roger" == Roger Bivand <Roger.Bivand at nhh.no> > >>>>>> on Wed, 29 Sep 2004 15:09:17 +0200 (CEST) writes: > [snip] > > > Roger> Perhaps use the names= argument (width can help too): > > ^^^^^^^^^^^^^^^^^^ > > Indeed! > > And that's why -- "in the good ol' times" when the box plot > was invented > > and enhanced, the inventors thought about it. > > For that reason there's the 'varwidth = TRUE/FALSE' argument > > in boxplot() > > > > Note from help(boxplot) however that the inventors thought > > it wiser to make the width proportional to the SQRT of the > > sample size rather than the sample.size itself, i.e., > > 'varwidth = TRUE' and your proposal are not equivalent. > > > > >> boxplot(expend~stature, width=sample.size/length(expend), > > >> + names=paste(levels(stature), ", N=", > sample.size, sep="")) > > > > Here are the current proposals [for cut & paste]: > > > > library(ISwR) > > data(energy) > > attach(energy) > > > > ## 1 > > boxplot(expend~stature) > > sample.size <- tapply(expend, stature, length) > > ss.ch <- paste("N=", sample.size, sep="") > > mtext(ss.ch, at=1:length(unique(stature)), line=2, side=1) > > > > ## 2 (Roger) > > boxplot(expend~stature, width=sample.size/length(expend), > > names=paste(levels(stature), ", N=", sample.size, sep="")) > > > > ## 3 (Roger + Martin): > > boxplot(expend ~ stature, varwidth= TRUE, > > names=paste(levels(stature), ", N=", sample.size, sep="")) > > Thanks for the explanation and the nice summary Martin! I can see > the point you're making about varwidth. I've read that part in > the documentation before but I have to admit that up to now I > didn't see the purpose of this parameter. Although there are > situations were I prefer to see the number in print somewhere on > the plot which I can now easily accomplish with `names'. > > Also thanks to Stephano for the pointer to the r-newsletter > article and to Don for showing me how one implements user > defined functions! > > Cheers > > Patrick > -- > For animals, the entire universe has been neatly divided into things > to (a) mate with, (b) eat, (c) run away from, and (d) rocks. > -- (Terry Pratchett, Equal Rites) > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > R-project.org/posting-guide.html >LEGAL NOTICE\ Unless expressly stated otherwise, this messag...{{dropped}}
> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of Patrick > Drechsler > > Gregory R. Warnes wrote on 01 Oct 2004 14:52:05 MET: > > [...] > > > Also note that boxplot.n in the gplots library (part of the > > gregmisc bundle) automatically adds the number of observations. > > Thanks Greg! Nice to see that you've written some code just for > this purpose. I will of course also take a closer look at the > other functions that are bundled in `gregmisc'. >:^)> Since I'm still new to R: can somebody give me a pointer to the > docs where to find instructions on a package (not a function)? I > can find the man pages to specific functions with ?<functionname> > (something similar to `texdoc <packagename>' in tetex)?library(help=<packagename>) you can also get the PDF package documentation available on cran, e.g. cran.r-project.org/doc/packages/gregmisc.pdf -Greg> > TIA, > > Patrick > -- > Look Ma, this man can twist his fingers as if they were made of > rubber, isn't that amazing? -- Not really, he's been using Emacs > for years...! > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > R-project.org/posting-guide.html >LEGAL NOTICE\ Unless expressly stated otherwise, this messag...{{dropped}}