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

Deepayan Sarkar deepayan.sarkar at gmail.com
Fri Apr 27 04:03:38 CEST 2007


On 4/26/07, Michael Kubovy <kubovy at virginia.edu> wrote:
> Hi Sundar,
>
> Thanks for taking a look at this
>
> On Apr 26, 2007, at 1:05 PM, Sundar Dorai-Raj wrote:
>
> >
> >
> > Michael Kubovy said the following on 4/26/2007 7:20 AM:
> >> Dear R-helpers,
> >> How do I tell panel.polygon what greoup and panel it applies to
> >> whithin
> >> xyplot(y ~ x | c, groups = g
> >>      panel = function(x, y, groups, ...){
> >>              panel.polygon(x = xpol[c, g], y = ypol[c, g],
> >> default.units  = 'native')
> >>              panel.xYplot(x, y, groups, ...)
> >>              llines(x = c(1, 6),  y = c(-24.283333, 35.941667), lwd = 2, lty
> >> =  3, col = 4)
> >>      }
> >> x[c, g] and y[c, g] describe the polygon I want plotted for group
> >> g  in panel c.
> >> _____________________________
> >
> > I believe you can just do:
> >
> > panel = function(x, y, subscripts, groups, ...) {
> >   ## note addition of subscripts to arguments
> >   panel.superpose(xpol, ypol, subscripts, groups,
> >                   panel.groups = "panel.polygon",
> >                   default.units = "native", ...)
> >   panel.xYplot(x, y, subscripts, groups, ...)
> >   llines(x = c(1, 6),  y = c(-24.283333, 35.941667),
> >         lwd = 2, lty = 3, col = 4)
> > }
> >
> > Also, it would be easier to provide tested answers if you gave a
> > reproducible example.
>
> Here is a reproducible example in which I tried (unsuccesfully) to
> apply your suggestion:
>
> 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)
>
> xyplot(est ~ x | cond, group = grp, data = data, type = 'b',
>      panel = function(x, y, subscripts, groups, ...){
>          panel.superpose(c(x, rev(x)), c(upper, rev(lower)),
> subscripts, groups,
>              panel.groups = 'panel.polygon', default.units =
> 'native', ...)
>          panel.xyplot(x, y, subscripts, groups, ...)
>      }
> )
> # returns: "Error in validGP(list(...)) : Must specify only one of
> 'font' and 'fontface'", which is incomprehensible to me.

Yeah, it's happening because of this:

> grid::gpar(font = 1, fontface = NULL)
Error in validGP(list(...)) : Must specify only one of 'font' and 'fontface'

panel.polygon needs a fix. Replace it by this:

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,
                           ...)
           panel.xyplot(x, y, ...)
       })

You are also better off not having arguments you are not using (your
code would have failed because of incorrect positional matching).

-Deepayan



More information about the R-help mailing list