[R] adding mean to boxplot

Andy Bunn abunn at montana.edu
Thu Jan 22 19:33:20 CET 2004


This will add triangles at the mean value. To change the behavior of
boxplot() to draw means instead of medians would involve rewriting the
bxp() code I believe. You could change the points in the code below to
segments. See ?segments. The archives have quite a few examples of
people modifying the bxp() code so look there and see ?bxp.

     # Load some data
     data(OrchardSprays)
     # Make the boxplot and save the boxplot object
     rb <- boxplot(decrease ~ treatment, data = OrchardSprays)
     # Compute the means
     mean.value <- tapply(OrchardSprays$decrease,
OrchardSprays$treatment, mean)
     # Add them as triangles.
     points(seq(rb$n), mean.value, pch = 17)

HTH, Andy




More information about the R-help mailing list