[R] lattice : using both strip and strip.left

Deepayan Sarkar deepayan.sarkar at gmail.com
Tue Aug 26 00:07:49 CEST 2008


On Mon, Aug 25, 2008 at 4:47 AM, baptiste auguie <ba208 at exeter.ac.uk> wrote:
> Dear all,
>
>
> I'm routinely using lattice and ggplot2, I wish to create a lattice theme
> that looks not too dissimilar to ggplot's defaults so I can include both
> graphs in a document with a consistent look.
>
> To illustrate my questions, consider the following example:
>
>> library(ggplot2)
>> library(lattice)
>>
>> # example data
>> x <- seq(0, 10, len = 100)
>> y1 <- jitter(sin(x), 1000)
>> y2 <- 0.5*jitter(cos(x), 1000)
>>
>> df <- melt( data.frame(x=x, one=y1, two=y2, condition1=factor(c("a",
>> "b")), condition2=factor(c("1","1", "2", "2"))),
>>                        id=c("x", "condition1", "condition2"))
>>
>> # custom colors
>> myColors <- c( "#E41A1C", "#377EB8")
>>
>> # here is the ggplot2 version
>> p <- qplot(x,value, data=df, facets = condition1 ~ condition2,
>> colour=variable)
>> p <- p + scale_colour_manual(values = myColors)
>> print(p)
>>
>> # lattice version
>> lattice.options(default.theme = canonical.theme(color = T))
>> trellis.par.set("strip.border" = list(col="white"))
>> trellis.par.set("background" = list(col="white"))
>> trellis.par.set("axis.line" = list(col="darkgrey"))
>> trellis.par.set("strip.background" = list(col="darkgrey"))
>> trellis.par.set("plot.symbol" = list(col = myColors, pch=16, cex=0.8))
>>
>>
>> p2 <- xyplot(value ~ x | condition1 + condition2, data=df, groups =
>> variable,
>>        strip=strip.custom(which.given=1),
>> strip.left=strip.custom(which.given=2)  ,
>>       panel = function(x, y, ...) {
>>                panel.fill(grey(0.95), border="white")
>>                panel.grid(h=-1, v=-1, col="white", col.line="white",
>> lty=1, lwd=2.5)
>>         lpoints(x, y, pch=16, col=1:2, cex=1)
>>       },
>>  key = simpleKey(levels(df$variable), space = "right"))
>>
>> print(p2)
>
> Several things resist me, I welcome any input,
>
> - with two levels of facetting, i often find convenient to layout the graphs
> in a 2d, rectangular matrix, and have vertical and horizontal strips as in
> ggplot2. Using strip and strip.left as in the example above leaves some
> blanks where the other strip is expected. Can this be tuned?

Yes; the simplest solution is to use the useOuterStrips() function
from latticeExtra.

> - in addition to the main grid, I'd like to set a finer, secondary grid,
> that subdivides it in halves. I can't find how to do this using panel.grid

If you mean automatically, then panel.grid() allows you to specify the
'n' parameter to pretty(). This works for the x-axis in your example
(see below), but not the y-axis. For finer control, you need to
specify the locations explicitly (probably using panel.abline).

> - the axes should be white, but trellis.par.set("axis.line" =
> list(col="white")) removes the tick marks altogether. Is there a way to get
> rid off the line but keep the tick marks? There is a new setting for this in
> base graphics, I guess there must be one in lattice with an appropriate call
> to grid?

There probably should be one (maybe called "axis.tick"), but there
isn't yet. A workaround is to use scales$col (see below).

> - to clip the background color to the plotting region as opposed to the
> whole page, I use panel.fill, maybe there's a better way (a setting outside
> the plotting function)?

Not really. You could change the default panel function if you want.

Here's a modified version of your example:

# lattice version
lattice.options(default.theme = canonical.theme(color = T))
trellis.par.set("strip.border" = list(col="white"))
trellis.par.set("background" = list(col="white"))
trellis.par.set("axis.line" = list(col="white"))
trellis.par.set("strip.background" = list(col="darkgrey"))
trellis.par.set("superpose.symbol" = list(col = myColors, pch=16, cex=0.8))

library(latticeExtra)

p2 <-
    xyplot(value ~ x | condition2 + condition1,
           data=df, groups = variable,
           panel = function(x, y, ...) {
               panel.fill(grey(0.95), border="white")
               panel.grid(h=-10, v=-10,
                          col="white",
                          lty=1, lwd=1)
               panel.grid(h=-5, v=-5,
                          col="white",
                          lty=1, lwd=2.5)
               panel.superpose(x, y, ...)
           },
           as.table = TRUE,
           between = list(x = 0.1, y = 0.1),
           scales = list(col = "darkgrey"),
           auto.key =
           list(space = "right", title = "variable"))

print(useOuterStrips(p2))

-Deepayan



More information about the R-help mailing list