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

Michael Kubovy kubovy at virginia.edu
Fri Apr 27 05:00:17 CEST 2007


Hi Sundar,

On Apr 26, 2007, at 10:03 PM, Deepayan Sarkar wrote:

> 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).

I'm not sure I understand.

I'm trying to add a filled band to show CIs around the data (which is  
why I tried to use xYplot, where 'filled bands' have bug).
So with these data:

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)

  I'm trying to decorate this with bands:

xyplot(est ~ x | cond, group = grp, data = data, type = 'b')

So I'm not sure why your proposal is failing:

my.panel.polygon <- function(..., font, fontface)
{
    panel.polygon(...)
}

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 = 'my.panel.polygon', default.units =  
'native', ...)
         panel.xYplot(x, y,,...)
     }
)

_____________________________
Professor Michael Kubovy
University of Virginia
Department of Psychology
USPS:     P.O.Box 400400    Charlottesville, VA 22904-4400
Parcels:    Room 102        Gilmer Hall
         McCormick Road    Charlottesville, VA 22903
Office:    B011    +1-434-982-4729
Lab:        B019    +1-434-982-4751
Fax:        +1-434-982-4766
WWW:    http://www.people.virginia.edu/~mk9y/



More information about the R-help mailing list