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

2009-11-01 Thread Deepayan Sarkar
On Fri, Oct 30, 2009 at 8:33 PM, Ottorino-Luca Pantani
 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() /
>> /                                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@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.
>

__
R-help@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.


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

2009-05-07 Thread Deepayan Sarkar
On Thu, May 7, 2009 at 2:50 PM, Daniel Kornhauser  wrote:
> Hi:
>
> Here is simplified example of what I am having trouble with:
> I want to set the gray shade of the background of each panel of a
> xyplot with its mean.
> My aim is to be able to compare at a glance which panel has the highest mean.
> But, in order to achieve this I have to normalize the means between 0 and 1.
> The normalization is necessary to stay within the valid range for the
> gray() command.
> I can’t figure out how to do it :-(
>
> Here is an example where I use max(Oats$yield) (the all around maximum
> value) to normalize.
> Note that what I am looking for is an expression what would mean
> something like this as max()
>
> # Start Example, just copy and paste
> 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()
>                                metric = eval(mean(y)/max(Oats$yield))
>                                panel.fill(col = gray(metric))
>                                panel.lines(x,y)
>                          }
>                        )
> print(tp1.oats)
> # End Example
>
> Does anybody have any suggestion how I can get max() ?

The simple answer is to use tapply():

> with(Oats, tapply(yield, list(Variety, Block), mean))
 I IIIIIIV V VI
Golden Rain 133.25 113.25  86.75 108.0 95.50  90.25
Marvellous  129.75 121.25 118.50  95.0 85.25 109.00
Victory 143.00  87.25  82.50  91.5 92.00  89.50

So,

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)
   })


For the general problem of sharing/aggregating per-panel information,
you can use the prepanel function, which is guaranteed to run once for
every panel (or rather every packet, to be more precise) with the same
arguments as the panel function. This example takes advantage of the
persistence of environments:

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

__
R-help@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.