[R] Lattice Groups

Deepayan Sarkar deepayan.sarkar at gmail.com
Fri Apr 30 19:51:09 CEST 2010


On Thu, Apr 29, 2010 at 6:15 PM, Santosh <santosh2005 at gmail.com> wrote:
> Richard,
> Thanks for your email. I am not looking for that kind of plot as you had
> suggested. I would like to see overlaid boxplots. Deepayan had earlier shown
> the method of overlay of boxplots (using panel.groups=panel.bwplot)...
>
> I have seen in some packages like ggplot2, where the overlapped plots are
> automatically shown as a mix of overlapping colors, and was wondering if
> similar features can be implemented in lattice package...

As you have figured out, the trick is to change the default settings
within the panel.groups function (this is because certain parameters
in panel.bwplot are taken from the settings and cannot be overridden
using function arguments). Here is a cleaner implementation of that
idea:

my.settings <-
    list(box.dot = list(col = c("red", "blue"), pch = 16),
         box.rectangle = list(col = c("red", "blue"),
                              fill = c("pink", "lightblue"),
                              alpha = c(0.5, 0.5)),
         box.umbrella = list(col = c("red", "blue")))

bwplot(y ~ factor(category),
       groups = level,
       data = tmp,
       panel = panel.superpose,
       panel.groups = function(x, y, group.number, ..., horizontal) {
           opar <- trellis.par.get()
           on.exit(trellis.par.set(opar))
           trellis.par.set(list(box.dot = Rows(my.settings$box.dot,
group.number),
                                box.rectangle =
Rows(my.settings$box.rectangle, group.number),
                                box.umbrella =
Rows(my.settings$box.umbrella, group.number)))
           panel.bwplot(x, y, varwidth = TRUE, notch = TRUE,
horizontal = horizontal)
           panel.loess(x, y, lwd = 2, alpha=0.2, lty=1,
                       col.line = c("red","blue")[group.number])
           panel.abline(h=0, col="black", lty=2)
       })

-Deepayan

>
> **Reproducing the earlier posted code for convenience..**
>
> tmp <- data.frame(
> y=rnorm(100),
> category=rep(factor(letters[1:
> 5]),each=20),
> level=rep(factor(0:1), length=100))
>
> barchart(y~factor(category),
> groups=level,
>         data=tmp,jitter.x=F,
>         panel=function(...){
>         panel.superpose( ...)
>         panel.superpose(panel.groups=panel.bwplot,
>             alpha=c(0.5,0.5),
>             varwidth=T,notch=T,
>             col=c("red","blue"),
>             fill=c("pink","lightblue"),pch=16,
>
> par.settings=list(box.umbrella=list(col=c("red","blue"),box.dot=list(col=c("red","blue")))),...)
>
> panel.superpose(panel.groups=panel.loess,lwd=2,col.line=c("red","blue"),alpha=0.2,lty=1,...)
>         panel.abline(h=0,col="black",lty=2)},
>         xlab="time bin (week)",
>         auto.key=list(space="right",text=c("A","H"),points=T))
>
> Thanks,
> Santosh
>
>
> On Thu, Apr 29, 2010 at 1:58 PM, RICHARD M. HEIBERGER <rmh at temple.edu>
> wrote:
>>
>> Santosh,
>>
>> continuing with your example, I recommend several functions in the HH
>> package.
>>
>> ## install.packages("HH") ## needed once, if you don't already have it
>>
>> require(HH)
>>
>> tmp <- data.frame(y=rnorm(100),
>>                   category=rep(factor(letters[1:5]), each=20),
>>                   level=rep(factor(0:1), length=100))
>> bwplot(y ~ interaction(level, category), data=tmp, col=c("red","blue"),
>>        panel=panel.bwplot.intermediate.hh)
>> tmp$lv <- with(tmp, interaction(level, category))
>> position(tmp$lv) <- outer(c(-.3,.3), seq(1.5, 9.5, 2), `+`)
>> bwplot(y ~ lv, data=tmp, col=c("red","blue"),
>>        panel=panel.bwplot.intermediate.hh,
>>        scales=list(x=list(at=seq(1.5, 9.5, 2), labels=letters[1:5])))
>>
>> Rich
>>
>



More information about the R-help mailing list