[R] Adding mean values to boxplots

ilai keren at math.montana.edu
Thu Mar 15 16:52:08 CET 2012

You want to assign your call to boxplot as an object that contains the
plot information

b <- matrix(rgamma(100,(1:4)/2,.5),nc=4)
(bxp <- boxplot(b))

Now you can use the info in bxp for placement, e.g.:


By the way, the "line in the middle" is the median, not the mean.



On Thu, Mar 15, 2012 at 6:04 AM, Cleland <tom.albery at tribalgroup.com> wrote:
> Hello there,
> I was wondering if anyone might be able to help me as I'm pretty new to R.
> I'm trying to create a boxplot from a data table returned from an sproc. I
> have the following code, which generates the plot as I'd like it:
> library("RODBC");
> conn <- odbcConnect("datawarehouse"); # connect to datawarehouse
> results <- sqlQuery(conn, "call sproc)");
> ylab <- 'Percentage (%)'
> main = 'Productivity Overview - 6 mnths';
> par(mai=c(1.5,1,1,0.2), las = 2, xpd=TRUE, cex.axis=0.8, cex.lab=0.8);
> col = c('forestgreen', 'forestgreen', 'forestgreen', 'chocolate',
> 'chocolate', 'chocolate', 'goldenrod', 'goldenrod', 'goldenrod');
> b <- results[,4:12]
> boxplot(b, col=col, ylab=ylab, main=main, boxwex=0.4);
> What i'd like to do is add the mean value, demarcated by the black line in
> the middle of each box, to the plot. The only way I've managed to add values
> previously is by specifying their exact value and location on the plot e.g.
> text(1.4,2.139,format("Mean (2.14)",digits=2), cex=1);
> I need these values to be dynamic in the sense that they will be different
> each time the graph is generated (based on the parameters passed through the
> sproc) and will need to be in a different position each time (i.e. in-line
> with the mean for each box).
> I'm uncertain about a. whether this is possible in R and b. if it is
> possible, how best I could achieve it.
> If anyone has any advice it would be gratefully received.
> Thanks in advance.
> Tom
> --
> View this message in context: http://r.789695.n4.nabble.com/Adding-mean-values-to-boxplots-tp4474700p4474700.html
> Sent from the R help mailing list archive at Nabble.com.
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list