Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boxplot in R showing the mean

Tags:

plot

r

boxplot

Does anybody know of a way of generating a boxplot in R with a line (or another symbol) in the value corresponding to the mean?

Thank you!

like image 399
Brani Avatar asked Mar 22 '10 14:03

Brani


3 Answers

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)

like image 177
James Avatar answered Oct 25 '22 22:10

James


With ggplot2:

p<-qplot(spray,count,data=InsectSprays,geom='boxplot')
p<-p+stat_summary(fun.y=mean,shape=1,col='red',geom='point')
print(p)
like image 20
Jyotirmoy Bhattacharya Avatar answered Oct 25 '22 21:10

Jyotirmoy Bhattacharya


Check chart.Boxplot from package PerformanceAnalytics. It lets you define the symbol to use for the mean of the distribution.

By default, the chart.Boxplot(data) command adds the mean as a red circle and the median as a black line.

Here is the output with sample data; MWE:

#install.packages(PerformanceAnalytics)    
library(PerformanceAnalytics)
chart.Boxplot(cars$speed)

picture of a boxplot using performance analytics package

like image 20
George Dontas Avatar answered Oct 25 '22 22:10

George Dontas