[R] displaying sample size in boxplots

Martin Maechler maechler at stat.math.ethz.ch
Wed Sep 29 18:11:13 CEST 2004


>>>>> "Roger" == Roger Bivand <Roger.Bivand at nhh.no>
>>>>>     on Wed, 29 Sep 2004 15:09:17 +0200 (CEST) writes:

    Roger> 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)
    >> 

    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=""))




More information about the R-help mailing list