Skip to content

How to include CI in a grouped barplot?

5 messages · Thais Rangel, Marc Schwartz, Rui Barradas +1 more

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

            
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
#
Hello,

If I understand it right, you can use the arrows() function with an 
angle of 90 to get ci bars.
Using your data example, but with made up standard errors,


a=c(10,15)
b=c(20,24)
c=c(21,23)

hei=cbind(a,b,c)

# Standard errors
sigma <- matrix(runif(6), ncol = 3)
# helper function
ci <- function(x, conf = 0.95) x*qnorm(1 - (1 - conf)/2)

lo <- hei - ci(sigma)
hi <- hei + ci(sigma)

graph1=barplot(hei, beside=T, ylim = c(0, max(hi)))
sapply(1:ncol(graph1), function(j){
     arrows(graph1[,j], hei[,j], graph1[,j], lo[,j], length = 0.15, 
angle = 90)
     arrows(graph1[,j], hei[,j], graph1[,j], hi[,j], length = 0.15, 
angle = 90)
})


Hope this helps,

Rui Barradas
Em 08-11-2012 13:02, Thais Rangel escreveu:
3 days later