[R] displaying sample size in boxplots

Marc Schwartz MSchwartz at MedAnalytics.com
Wed Sep 29 15:08:19 CEST 2004


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




More information about the R-help mailing list