[R] Drawing rectangles in multiple panels

hadley wickham h.wickham at gmail.com
Thu Jul 12 06:41:45 CEST 2007


On 7/12/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().

This is an interesting approach.  The one problem I see with it is
that if you change the trellising specification, you have to change
your rectInfo datastructure.  I guess we're missing the code that
actually generates rectInfo in the first place, so maybe in practice
it's not such a big problem.

Hadley



More information about the R-help mailing list