[R] add custom strip to lattice plot

Patrick Drechsler patrick at pdrechsler.de
Thu Aug 2 08:56:56 CEST 2007


"Deepayan Sarkar" <deepayan.sarkar at gmail.com> writes:

> On 7/30/07, Patrick Drechsler <patrick at pdrechsler.de> wrote:

>> 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'.

Thank you very much for your feedback Deepayan! This got me on the
correct track.

Cheers,

Patrick



More information about the R-help mailing list