[R] xyplot() and controlling panel.polygon()

Deepayan Sarkar deepayan.sarkar at gmail.com
Fri Apr 27 20:25:07 CEST 2007


On 4/27/07, Michael Kubovy <kubovy at virginia.edu> wrote:
> Hi Deepayan,
>
> Thanks for your advice. This is moving along, however:
> The following is drawing the same polygons in each panel. I'm trying
> to get a different polygon (confidence band) for each group in each
> panel. That's why I thought I would need to pass groups and
> subscripts to the panel.groups
>
> est <- c(1:4, 3:6, 7, 9, 11, 13, 12, 15, 18, 21)
> cond <- rep(c('a','b'), each = 8)
> grp <- rep(c('I', 'II'), each = 4, 2)
> x <- rep(c(.5, .7, .9, 1.1), 4)
> upper <- est + 1
> lower = est - 1
> data <- data.frame(est = est, x = x, cond = cond, grp = grp, upper =
> upper, lower = lower)
>
> my.panel.polygon <- function(..., font, fontface)
> {
>     panel.polygon(...)
> }
>
> xyplot(est ~ x | cond, group = grp, data = data, type = 'b',
>      panel = function(x, y, ...){
>          panel.superpose(c(x, rev(x)), c(upper, rev(lower)),
>              panel.groups = 'my.panel.polygon', default.units =
> 'native', ...)
>          panel.xyplot(x, y, ...)
>      }
> )
>
> It's pretty clear that panel.superpose is not getting its x and y
> values after they are split by group and panel.

You are not even trying to do that; you have

  panel.superpose(c(x, rev(x)), c(upper, rev(lower)), <...>

so your x=c(x, rev(x)) is not the same length as x (and subscripts), and your
y = c(upper, rev(lower)) is not the same length as anything. Also,
your upper and lower are being taken from the global env, not data
(they happen to be the same, but since your data has them, I assume
you want to use them).

Perhaps you are looking for something like this:


panel.bands <-
    function(x, y, upper, lower,
             subscripts, ..., font, fontface)
{
    upper <- upper[subscripts]
    lower <- lower[subscripts]
    panel.polygon(c(x, rev(x)), c(upper, rev(lower)), ...)
}

xyplot(est ~ x | cond, group = grp, data = data, type = 'b',
       upper = data$upper,
       lower = data$lower,
       panel = function(x, y, ...){
           panel.superpose(x, y, panel.groups = 'panel.bands', ...)
           panel.xyplot(x, y, ...)
       })

-Deepayan



More information about the R-help mailing list