Skip to content

adding mean to boxplot

4 messages · Johannes Ullrich, Andy Bunn, Marc Schwartz +1 more

#
I am a new and unexperienced user of R and got so far as to know how to produce 
boxplots. I have no experience of messing with function code, so presently I do 
not know how to create a boxplot with group means instead of group medians. If 
somebody could help me either replace the median with the mean or superimpose 
the mean onto the existing boxplot, it would be appreciated.
#
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
#
On Thu, 2004-01-22 at 12:11, Johannes Ullrich wrote:
You are probably better doing the second option, since boxplots are
premised on the visual display of quantiles.

Here is a quick example of adding means:

# Create two groups of continuous data
# and bind them together
A <- data.frame(Group = "A", 
                Measure = rnorm(50, 5))

B <- data.frame(Group = "B", 
                Measure = rnorm(50, 7.5))
Data <- rbind(A, B)
attach(Data)

# Now create a boxplot with two groups using
# the formula method
boxplot(Measure ~ Group)

# Get the group means
means <- by(Measure, Group, mean)                        

# Plot symbols for each mean, centered on
# x = 1 and x = 2, which are the default center
# values.
points(1:2, means, pch = 23, cex = 0.75,
       bg = "red")

# Now label the means, formatting the values
# to one decimal place. Place the values to the
# left of each group plot.
text(1:2 - 0.4, means, 
     labels = formatC(means, format = "f", 
                      digits = 1),
     pos = 2, cex = 0.9, col = "red")

# Clean up
detach(Data)

See ?boxplot, ?par, ?points, ?text and ?formatC for more information.

You might want to review R News (http://cran.r-project.org/doc/Rnews/)
Vol 3  Number 2 (October 2003), where there is an article providing an
introduction to R's base graphics.

HTH,

Marc Schwartz
#
On Thu, 22 Jan 2004 19:11:08 +0100
Johannes Ullrich <ullrichj at mailer.uni-marburg.de> wrote:

            
You might want to also consider extended box plots or box-percentile plots
to show more information, plus the mean.  See the panel.bpplot function in
the Hmisc package.  There are many examples in the help file.
---
Frank E Harrell Jr   Professor and Chair           School of Medicine
                     Department of Biostatistics   Vanderbilt University