[R] add custom strip to lattice plot

Deepayan Sarkar deepayan.sarkar at gmail.com
Wed Aug 1 21:59:02 CEST 2007


On 7/30/07, Patrick Drechsler <patrick at pdrechsler.de> wrote:
> Hi,
>
> what is the recommended way of adding a strip to a lattice plot?

See ?strip.default.

> In the example below I would like to add the value of mean(y) to a new
> strip.:
>
> --8<---------------cut here---------------start------------->8---
> library(lattice)
>
> ## Small sample data set:
> p0 <- xyplot(uptake ~ Type | Treatment, data = CO2)
>
> p1 <- update(p0,
>              panel = function(x, y, ...) {
>                panel.xyplot(x, y)
>                panel.abline(h = mean(y),
>                             col = "red"
>                             )
>              }
>              )
>
> plot(p1)
> --8<---------------cut here---------------end--------------->8---

That's not what strips are for. Strips are used for showing
information about the levels of conditioning variables. You can
instead do something like this:

update(p0,
       panel = function(x, y, ...) {
           require(grid)
           panel.xyplot(x, y, ...)
           panel.abline(h = mean(y), col = "red")
           grid.text(round(mean(y), 3), 0.5, 1, vjust = 1.2)
       })

The text can be put into a rectangle, to make them look like strips,
and you could use a prepanel function to make sure there's enough
space for it. Look up grid documentation for details.

There are other workarounds, e.g., using 'trellis.focus' and
'lattice.panelArgs'.

-Deepayan



More information about the R-help mailing list