Sorry for the second post. I caught a few glitches in the code from my previous post. I've also made a little bit of progress in trying to figure out why I can't get error bars to line up properly. It appears that the subscripts are only passing two values, the center of each group. There should be six values, one for the center of each bar (correct?), but I have no idea how to fix that or what I've done wrong. Here's the (corrected) code I've got so far...

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)
colnames(growth.means)[3] <- "growth"

library(lattice)

panel.ci <- function(x, y, subscripts, ...){
         panel.barchart(x, y, horiz=F, subscripts=subscripts, groups=growth.means$Group.2, ...)
         panel.segments(x[subscripts], y, x[subscripts], y+0.5)
         print(x[subscripts])
     }

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

Begin forwarded message:

> From: Nathan Lemoine <lemoine.nathan@gmail.com>
> Date: March 2, 2012 11:53:24 PM EST
> To: r-help@r-project.org
> Subject: Grouped barchart confidence intervals in lattice
> 
> 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
> 
> 
> 
> 
> 
> 
> 


	[[alternative HTML version deleted]]

