[R] adding mean to boxplot
Marc Schwartz
MSchwartz at medanalytics.com
Thu Jan 22 19:47:56 CET 2004
On Thu, 2004-01-22 at 12:11, Johannes Ullrich wrote:
> 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.
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
More information about the R-help
mailing list