[R] Graphically centering confidence interval in barplot

Marc Schwartz MSchwartz at mn.rr.com
Thu Jun 30 14:53:10 CEST 2005


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




More information about the R-help mailing list