Adding mean values to boxplots

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Adding mean values to boxplots

Cleland
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
Reply | Threaded
Open this post in threaded view
|

Re: Adding mean values to boxplots

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

set.seed(1)
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.:

text(1:length(bxp$names),bxp$stats[3,],round(bxp$stats[3,],2),pos=3)

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

 points(apply(b,2,median),col=2,pch='x')
 points(colMeans(b),col=2)


HTH


On Thu, Mar 15, 2012 at 6:04 AM, Cleland <[hidden email]> 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Adding mean values to boxplots

Cleland
Great. Thanks very much for that... and for pointing out the obvious error! Much appreciated.
Tom
Reply | Threaded
Open this post in threaded view
|

Re: Adding mean values to boxplots

Cleland
In reply to this post by ilai-2
That works great by the way. Thanks very much for your help!