[R] superpose.polygon, panel.polygon and their colors

ottorino ottorino-luca.pantani at unifi.it
Tue Oct 19 18:29:02 CEST 2010


Dear R-helpers,
the problem I'm facing today is to convince lattice to paint some areas
in gray.
The areas I would like to have in gray, are confidence bands

I've googled around in the mailing list archives and eventually find
some clues.

This link is my starting point
http://tolstoy.newcastle.edu.au/R/e2/help/07/04/15595.html

I'm reproducing here the code for your convenience

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)
rm(est, cond, grp, x, upper,lower)

panel.bands <-

    function(x, y, upper, lower,

             subscripts, col, ..., 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, ...)
       })

The result is a lattice object with the confidence bands painted in cyan
and pink. These are the areas I would like to have in gray.

I think that the cyan and pink colors come from

trellis.par.get("superpose.polygon")[[2]][1:2]

To change the colors I tried, unsuccessfully, the following 4 ways:

1)
trellis.par.set("superpose.polygon", list(col="gray"))
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, ...)
       })

2)
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',
col="gray", ...)
           panel.xyplot(x, y, ...)
       })

3)
ltheme <- canonical.theme(color = FALSE)
ltheme$superpose.polygon$col="gray"
xyplot(est ~ x | cond, group = grp, data = data, type = 'b',
       upper = data$upper,
       lower = data$lower,
       theme=ltheme,
       panel = function(x, y, ...){
           panel.superpose(x, y, panel.groups = 'panel.bands',...)
           panel.xyplot(x, y, ...)
       })

4)
panel.bands.1 <-

    function(x, y, upper, lower,

             subscripts, col, ..., font, fontface) {

    upper <- upper[subscripts]
    lower <- lower[subscripts]
    panel.polygon(c(x, rev(x)), c(upper, rev(lower)),
    col="gray", ...) }

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

I suspect that superpose polygon is not involved at all in the process,
and in

test.gray <- 
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, ...)
       })
str(test.gray)

I cannot find any indication on the colors.

Strangely enough, the following code seems to modify something, 
the border of the colored areas.

panel.bands.2 <-

    function(x, y, upper, lower,

             subscripts, col, ..., font, fontface) {

    upper <- upper[subscripts]
    lower <- lower[subscripts]
    panel.polygon(c(x, rev(x)), c(upper, rev(lower)),
    border= 2, ...) }

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

In other words I can modify the borders, but not the shaded areas.
This sounds strange to me.
Where am I wrong ?

Thanks in advance for your time.


-- 
Ottorino-Luca Pantani, Università di Firenze
Dip.to di Scienze delle Produzioni Vegetali,
del Suolo e dell'Ambiente Forestale (DiPSA)
P.zle Cascine 28 50144 Firenze Italia
Ubuntu 10.04 -- GNU Emacs 23.1.50.1 (x86_64-pc-linux-gnu, GTK+ Version
2.18.0)
ESS version 5.8 -- R 2.10.1



More information about the R-help mailing list