[R] background color in strip.custom()

Deepayan Sarkar deepayan.sarkar at gmail.com
Tue Dec 5 00:40:43 CET 2006


On 12/4/06, Sven Garbade <Sven.Garbade at med.uni-heidelberg.de> wrote:
> Hi all,
> how can I change the background color in lattice strips according to a
> factor level, eg:
>
> library(lattice)
> x <- rnorm(100)
> y <- sqrt(x)
> f <- gl(2, 50, c("A", "B"))
> xyplot(y ~ x | f)
>
> I like to change the background color of the strips according to the
> levels in f and tried several things like this with no success:
>
> xyplot(y ~ x | f, strip=strip.custom(bg=c("red", "green")))
>
> Is this possible?

Yes, you just have to write a custom strip function (slightly beyond
what strip.custom is capable of):

xyplot(y ~ x | f,
       strip = function(..., which.panel, bg) {
           bg.col = trellis.par.get("strip.background")$col;
           ## or bg.col = c("red", "green") if you prefer
           strip.default(..., which.panel = which.panel,
                        bg = rep(bg.col, length = which.panel)[which.panel])
       })

This will only work when you have one conditioning variable (otherwise
'which.panel' won't be a scalar), but you haven't told us what you
want to happen otherwise.

-Deepayan




More information about the R-help mailing list