abline(h=mean(x))
for a horizontal line (use v instead of h for vertical if you orient your boxplot horizontally), or
points(mean(x))
for a point. Use the parameter pch
to change the symbol. You may want to colour them to improve visibility too.
Note that these are called after you have drawn the boxplot.
If you are using the formula interface, you would have to construct the vector of means. For example, taking the first example from ?boxplot
:
boxplot(count ~ spray, data = InsectSprays, col = "lightgray")
means <- tapply(InsectSprays$count,InsectSprays$spray,mean)
points(means,col="red",pch=18)
If your data contains missing values, you might want to replace the last argument of the tapply
function with function(x) mean(x,na.rm=T)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…