[R] displaying sample size in boxplots

Patrick Drechsler patrick.drechsler at gmx.net
Wed Sep 29 19:47:26 CEST 2004

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:

>     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!


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)

More information about the R-help mailing list