[R] A question about boxplot: mean and median

Marc Schwartz MSchwartz at MedAnalytics.com
Fri Apr 15 18:20:01 CEST 2005


On Fri, 2005-04-15 at 08:59 -0700, R_xprt_wannabe wrote:
> Dear List,
> 
> I have worked through the examples given in the help
> on boxplot().  If I am reading it right, only the
> median is produced (as a default), which is
> represented by the horizontal line inside the box. 
> What argument do I need to specify if I want to show
> the mean as well?
> 
> Thanks,

There is no standard command using boxplot(). You need to add the means
using points():

# Create df with 5 columns
df <- as.data.frame(matrix(rnorm(50), ncol = 5))

# Now get the mean for each column
means <- apply(df, 2, mean, na.rm = TRUE)

# Plot the boxplot
boxplot(df)

# Add the means, use pch = 19 to differentiate
# between outliers and means
points(1:5, means, pch = 19)

Note that the boxes in boxplot are set at integer values on the x-axis
by default (presuming that you are using vertical boxes, otherwise the y
axis).

HTH,

Marc Schwartz




More information about the R-help mailing list