[R] How to include CI in a grouped barplot?

Marc Schwartz marc_schwartz at me.com
Thu Nov 8 18:18:32 CET 2012


On Nov 8, 2012, at 7:02 AM, Thais Rangel <thaisrangelnut at gmail.com> wrote:

> Hello everyone!
> 
> I need to include the confidence interval bar in a grouped barplot. I've
> found some options on the web, but none of them solved my problem.
> 
> The question is: my barplot was created using vectors for each pair of bar
> and them combining them using cbind.
> I mean:
> 
> a=c(10,15)
> b=c(20,24)
> c=c(21,23) ...
> 
> hei=cbind(a,b,c)
> graph1=barplot(hei, beside=T,...)
> 
> I've tried to include each CI in a vector just like the height of the bars,
> but it hasn't worked (!!)
> Is there an easy way to do this??
> 
> Thanks a lot =)
> 
> 
> Thais Rangel
> (Rio de Janeiro, Brazil)


You might take a look at the barplot2() function, which is in the gplots package on CRAN.

Alternatively, read the examples in ?barplot and take note of the example using the VADeaths data set, where it creates faked upper error bars (aka 'dynamite plots', which are shunned heavily). You would need to extend that example, using segments(), to draw the lower bounds and horizontal line segments as well.

The key hint is that barplot() returns the bar midpoints, which then allow you to position vertical lines for each bar center. 

A conceptual note, which is that this format can be ok for proportions/percentages, but you will recommendations against using this format to display continuous data (eg. means +/- SE). In that case, point plots with CI's is preferred. 

Regards,

Marc Schwartz




More information about the R-help mailing list