[R] Grouped barchart confidence intervals in lattice

ilai keren at math.montana.edu
Sun Mar 4 03:46:54 CET 2012


On Sat, Mar 3, 2012 at 5:48 PM, Nathan Lemoine <lemoine.nathan at gmail.com> wrote:
>  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?),

No. That's also why your code doesn't work. x[subscripts] are not the
centroids. To my knowledge there isn't a barchart equivalent to
boxplot.stats which means for placement you need to recalculate the
location of the bars, as is done internally in panel.barchart. This
panel function may need some tweaking depending on complexity of the
real data (for conditioning add [[packet.number()]] / for limits and
scales - a little more work) but hopefully help get you started:

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)
growth.errs <- aggregate(growth,list(coat,diet),sd)
require(lattice)
panel.ci <- function(x, y, sdl, groups, subscripts,box.ratio=box.ratio, ...){
     groupS <- as.numeric(groups[subscripts])
     nv <- nlevels(groups)
     w <- box.ratio/(nv*(1 + box.ratio))
     yy <- as.numeric(y)+sdl
     xx <- as.numeric(x)+(3/4)*w*(groupS - (nv + 1)/2)
     panel.barchart(x, y,groups=groups,subscripts=subscripts,...)
     panel.segments(xx,as.numeric(y),xx,yy,lwd=2)
   }
barchart(x~Group.1, groups=Group.2,data=growth.means,sdl=growth.errs$x,
         horiz=F,stack=F,
ylim=c(0,max(growth.errs$x+growth.means$x)+.2), panel=panel.ci)

Cheers
Elai


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 at gmail.com>
>> Date: March 2, 2012 11:53:24 PM EST
>> To: r-help at 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]]
>
> ______________________________________________
> 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