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]]
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
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.