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

Rui Barradas ruipbarradas at sapo.pt
Thu Nov 8 20:14:59 CET 2012


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:
> 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)
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list