# Re: [R] Graphically centering confidence interval in barplot

From: Marc Schwartz <MSchwartz_at_mn.rr.com>
Date: Thu 30 Jun 2005 - 22:53:10 EST

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 Smit

barplot() 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

R-help@stat.math.ethz.ch mailing list