[R] Grouped barchart confidence intervals in lattice

Nathan Lemoine lemoine.nathan at gmail.com
Sat Mar 3 05:53:24 CET 2012


Hi everyone,

I'm having trouble adding error bars to a grouped barchart in lattice. I know that this topic has been addressed quite a bit, as I've been searching the internet for a while to try to troubleshoot the issue, but I've not been able to find any solution that I could get working on my data. I was wondering if someone could look at my code and tell me what I'm doing wrong. I was hoping somebody's found a way to do this (I'm sure they have) and can tell me how to fix my code.

# Input example data

growth <- c(6.6,7.2,6.9,8.3,7.9,9.2,8.3,8.7,8.1,8.5,9.1,9.0)
diet <- as.factor(rep(c("A","B","C"),2,each=2))
coat <- as.factor(rep(c("light","dark"),each=6))

growth.means <- aggregate(growth,list(coat,diet),mean)

library(plotrix)

growth.errs <- aggregate(growth,list(coat,diet),std.error)

# Try using the superpose call with panel.groups results in an error

panel.ci <- function(x, y, subscripts, groups...){
         panel.barchart(x, y, groups=groups, subscripts=subscripts, horiz=F,...)
         panel.segments(x[subscripts], y, x[subscripts], y+growth.errs$x, col = 'black')
     }

barchart(growth~Group.1, groups=Group.2, data=growth.means,
         panel=panel.superpose,
         panel.groups=panel.ci
         )

# Try using the generic plot.barchart command gives three error bars, all are the appropriate sizes, but all are centered in each group and not on the grouped bars

barchart(x~Group.1, groups=Group.2, data=growth.means,
         panel=function(x,y,subscripts, groups){
           panel.barchart(x,y,horiz=F,groups=groups, subscripts=subscripts)
           panel.segments(as.numeric(x)[subscripts],y,as.numeric(x)[subscripts],y+growth.errs$x)
         }
         )

What am I doing wrong?

Thanks,

Nathan Lemoine



More information about the R-help mailing list