[R] Drawing rectangles in multiple panels

deepayan.sarkar at gmail.com deepayan.sarkar at gmail.com
Wed Jul 11 19:04:11 CEST 2007

On 7/11/07, Jonathan Williams <williams222 at llnl.gov> wrote:
> Hi folks,
> I'm having some trouble understanding the intricacies of panel
> functions.  I wish to create three side-by-side graphs, each with
> different data-- so far, so good: I rbind() the data, add a column of
> subscripts as a conditioning variable, load up the lattice package,
> specify either a c(3,1) 'layout' or work through 'allow.multiple' and
> 'outer' and I'm good to go.
> But now I wish to add three rectangles to each plot, which will be in
> different places on each panel, and I'm terribly stuck.  I can guess
> this requires defining a panel function on the fly, but none of my
> attempts are working.  Suggestions?

You haven't told us what determines the rectangles (only that they are
different in each panel). If they are completely driven by panel data,
here's an example:

panel.qrect <-
    function(x, y, ...)
    xq <- quantile(x, c(0.1, 0.9))
    yq <- quantile(y, c(0.1, 0.9))
    panel.rect(xq[1], yq[1], xq[2], yq[2],
               col = "grey86", border = NA)
    panel.xyplot(x, y, ...)

xyplot(Sepal.Length ~ Sepal.Width | Species, iris,
       panel = panel.qrect)

If the rectangles are somehow determined externally, you probably want
to use one of the accessor functions described in help(panel.number).
There are good and bad (i.e. less robust) ways to use these, but we
need to know your use case before recommending one.


More information about the R-help mailing list