[R] Grouped barchart confidence intervals in lattice

ilai keren at math.montana.edu
Sun Mar 4 23:28:08 CET 2012


packet.number() will retrieve the panel you are working in. With a
third conditioning variable, you now have say a list of *.err$x, one
for each panel/level of 3rd var. In that case you need
dsl[[packet.number()]] to get at the right place in the list (or
[,packet.number()] if it's a matrix etc.). Regarding the calculations,
you could type panel.barchart in the console to see Deepayan's
original. Sorry but I just don't remember what my logic was when
figuring it out (it has been a short while since my last barchart),
was probably just trial and error until it worked.
Best,

On Sun, Mar 4, 2012 at 1:15 PM, Nathan Lemoine <lemoine.nathan at gmail.com> wrote:
> Also, can you tell me where to add the [[packet.number()]] command for condition? I've been trying several things with no luck.
>
> Thanks for helping, sorry if I'm a nuisance, but I'm still learning my way around the more advanced functions and there's no one else at my university that uses R as extensively as I do.
>
> Nathan

On Sun, Mar 4, 2012 at 11:45 AM, Nathan Lemoine
<lemoine.nathan at gmail.com> wrote:
> Thanks! I really appreciate the help, I don't think I ever would have figured that out.
>
> If you don't mind, I like to understand exactly whats going on in the code.
>
> I see groupS identifies the start center of each group and identifies which group each row belongs to
> w calculates the half-width of each bar with respect to the whole group of bars (i.e. a bar with a width of 1in a group of three bars has a half width of 1/6)
> groupS - (nv+1)/2 gives each bar's relative position to the central bar
>
> Am I interpreting these correctly? One thing I don't get is where 3/4 came from.
>
> Anyway, thanks for all of the help. Wouldn't have gotten it on my own
>
> Nathan
>
>
> On Mar 3, 2012, at 9:46 PM, ilai wrote:
>
>> 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