[R] How to properly shade the background panels of an xyplot?

Deepayan Sarkar deepayan.sarkar at gmail.com
Sun Nov 1 13:46:29 CET 2009


On Fri, Oct 30, 2009 at 8:33 PM, Ottorino-Luca Pantani
<ottorino-luca.pantani at unifi.it> wrote:
> Dear R users,
> this is a follow up of this message
> http://tolstoy.newcastle.edu.au/R/e6/help/09/05/13897.html
> I'm reproducing the core of it for convenience.
>
>> //
>> / data(Oats, package = "MEMSS") /
>> / tp1.oats <- xyplot(yield ~ nitro | Variety + Block, /
>> /                      data = Oats, /
>> /                      panel = function(x, y, subscripts, ...) { /
>> /                                # How to normalize my heatmap metric with
>> /
>> /                                # the value of the panel that has maximum
>> average ? /
>> /                                # metric = eval(mean(y)/
>> max(<mean-of-each-panel>))))) /
>> /                                metric = eval(mean(y)/max(Oats$yield)) /
>> /                                panel.fill(col = gray(metric)) /
>> /                                panel.lines(x,y) /
>> /                          } /
>> /                        ) /
>> / print(tp1.oats) /
>> //
>>
>> xyplot(yield ~ nitro | Variety + Block,
>>
>>       data = Oats,
>>       max.mean = max(with(Oats, tapply(yield, list(Variety, Block),
>> mean))),
>>       panel = function(x, y, subscripts, max.mean, ...) {
>>           metric = mean(y)/max.mean
>>           panel.fill(col = gray(metric))
>>           panel.lines(x,y)
>>       })
>>
>>
>> or
>>
>> xyplot(yield ~ nitro | Variety + Block,
>>
>>       data = Oats,
>>       aux.env = new.env(parent = emptyenv()),
>>       prepanel = function(x, y, aux.env, ...) {
>>           aux.env$max.mean.y <- max(aux.env$max.mean.y, mean(y))
>>           list()
>>       },
>>       panel = function(x, y, subscripts, aux.env, ...) {
>>           metric = mean(y) / aux.env$max.mean.y
>>           panel.fill(col = gray(metric))
>>           panel.lines(x,y)
>>       })
>>
>> -Deepayan
>
> The result is a trellis object in which the background colour of the panels
> is an outcome of the data contained in the panel itself. After all, this is
> what "panel = function (x,y ....." is meant for, right ?
>
> But what, if I want to highlight some panels ? Arbitrarily or conditioned by
> another variable.
>
> Say I want to shade in gray only the upper right panels (Block VI, Victory
> and Marvellous varieties )

See ?which.packet, which will tell you the current levels of the
conditioning variables. So something like

      panel = function(x, y, subscripts, aux.env, ...) {
          wp <- which.packet()
          if (levels(Oats$Variety)[wp[1]] %in% c("Victory",
"Marvellous") || ...)
              panel.fill(col = gray(metric))
         panel.lines(x,y)
     })

-Deepayan

>
> Given a data frame like this, with a variable intended to set the colour of
> the panel background
> /
> data(Oats, package = "MEMSS")
> /Oats1 <- cbind.data.frame(Oats,
>                            Highlight =
>                            ifelse(Oats$Block == "VI" &
>                                   Oats$Variety %in% c("Victory",
> "Marvellous" ),
>                                   "gray",
>                                   "transparent")
>                            )
>
> which is a possible code ?
>
> I (more or less) know how to manage the data in the panel,
> but I cannot imagine how to do it with variables external to the panel
> itself.
>
> I suppose that the panel functions are not useful here.
> I'm wandering through par.settings, themes and panel.fill, but still without
> success.
> Any hint ?
>
> --
> Ottorino-Luca Pantani, Università di Firenze
> Dip. Scienza del Suolo e Nutrizione della Pianta
> P.zle Cascine 28 50144 Firenze Italia
> Ubuntu 8.04.3 LTS -- GNU Emacs 23.0.60.1 (x86_64-pc-linux-gnu, GTK+ Version
> 2.12.9)
> ESS version 5.5 -- R 2.9.2
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list