Smit, R. (Robin)
2005-Jun-30 12:40 UTC
[R] Graphically centering confidence interval in barplot
Hello, I have got a simple "cosmetic" question. I have created a bar plot with confidence intervals using: barplot(mean, ylim = c(0,0.2), las = 3, space = 0) arrows(1:17 ,X95p_low, 1:17, X95p_high, length = 0.07, angle = 90, code = 3, lty = 1) Unfortunately, the confidence bars align with the right side of the bars and I would like to shift these to the middle of the bars. Is there someone who can tell me how I can do this? Kind regards, Robin Smit This e-mail and its contents are subject to the DISCLAIMER at http://www.tno.nl/disclaimer/email.html [[alternative HTML version deleted]]
Marc Schwartz
2005-Jun-30 12:53 UTC
[R] Graphically centering confidence interval in barplot
On Thu, 2005-06-30 at 14:40 +0200, Smit, R. (Robin) wrote:> Hello, > > I have got a simple "cosmetic" question. > I have created a bar plot with confidence intervals using: > > barplot(mean, ylim = c(0,0.2), las = 3, space = 0) > > arrows(1:17 ,X95p_low, 1:17, X95p_high, length = 0.07, angle = 90, code > = 3, lty = 1) > > > > Unfortunately, the confidence bars align with the right side of the bars > and I would like to shift these to the middle of the bars. > > Is there someone who can tell me how I can do this? > > > > Kind regards, > Robin Smitbarplot() returns the bar midpoints, so you need to use these as your 'x' axis positions for the CI's: mp <- barplot(mean, ylim = c(0,0.2), las = 3, space = 0) arrows(mp, X95p_low, mp, X95p_high, length = 0.07, angle = 90, code = 3, lty = 1) Alternatively, you can use the barplot2() function in the 'gplots' package from CRAN: barplot2(mean, ylim = c(0,0.2), las = 3, space = 0, plot.ci = TRUE, ci.l = X95p_low, ci.u = X95p_high) BTW, you should avoid using 'mean' for your data vector, since that is also the name of a R function. R is smart enough to know the difference in most cases, but it could get you into trouble at some point. HTH, Marc Schwartz