[R] Drawing rectangles in multiple panels

Deepayan Sarkar deepayan.sarkar at gmail.com
Thu Jul 12 04:37:06 CEST 2007


On 7/11/07, Stephen Tucker <brown_emu at yahoo.com> wrote:
> In the Trellis approach, another way (I like) to deal with multiple pieces of
> external data sources is to 'attach' them to panel functions through lexical
> closures. For instance...
>
> rectInfo <-
>     list(matrix(runif(4), 2, 2),
>          matrix(runif(4), 2, 2),
>          matrix(runif(4), 2, 2))
>
> panel.qrect <- function(rect.info) {
>   function(x, y, ...) {
>     ri <- rect.info[[packet.number()]]
>     panel.rect(ri[1, 1], ri[1, 2], ri[2, 1], ri[2, 2],
>                col = "grey86", border = NA)
>     panel.xyplot(x, y, ...)
>   }
> }
>
> xyplot(runif(30) ~ runif(30) | gl(3, 10),
>        panel = panel.qrect(rectInfo))
>
> ...which may or may not be more convenient than passing rectInfo (and perhaps
> other objects if desired) explicitly as an argument to xyplot().

That's an interesting approach. I think the important thing is to make
sure that the data required to reproduce the plot is available as part
of the trellis object (e.g. if you save it and load it in another
session (not that anyone actually ever does that, but it's the
principle of the thing)). This happens transparently if you supply the
external data as arguments to xyplot(). It happens in your example
too, but the data is hidden inside the environment of the panel
function. The two approaches might differ in terms of memory use, but
I'm not sure.

-Deepayan



More information about the R-help mailing list