Skip to content

A question about boxplot: mean and median

2 messages · R_xprt_wannabe, Marc Schwartz

#
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,

platform i386-pc-mingw32
arch     i386           
os       mingw32        
system   i386, mingw32  
status                  
major    2              
minor    0.0            
year     2004           
month    10             
day      04             
language R
#
On Fri, 2005-04-15 at 08:59 -0700, R_xprt_wannabe wrote:
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