Skip to content

superpose.polygon, panel.polygon and their colors

11 messages · ottorino, Dennis Murphy, Dieter Menne +1 more

#
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 wrote:
Thanks for the code example and for all the work you already put into it!

I think this is an oversight in Deepayan's example, some collision between
... and explicitly passing col. Just remove the col from the argument list
of panel.bands

panel.bands <-  function(x, y, upper, lower,
           subscripts,  ..., font, fontface) { ### drop col
  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',
  col="gray", .## and add it here


Dieter
#
Il giorno mar, 19/10/2010 alle 11.12 -0700, Dieter Menne ha scritto:

Thanks Dieter for your help, but unfortunately your suggestion results
only in changing the color of the *lines* and not the color of the
*area* of the polygon.

I also tried to call "col" from within the panel.superpose

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", ...)###### here the change
           panel.xyplot(x, y, ...)
       })

still unsuccessfully.

Again, strangely enough, it accept the argument "border" but not the
argument "col".

Any other ideas ?

PS
Thanks also to Dennis Murphy for suggestions on ggplot2.

Il giorno mar, 19/10/2010 alle 11.12 -0700, Dieter Menne ha scritto:
#
Ottorino-Luca Pantani wrote:
The following complete code works for  me. Do you have the current version
of lattice/R installed?


# polygon color
library(lattice)
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,  ..., 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',
  col="gray", 
  upper = data$upper,
  lower = data$lower,
  panel = function(x, y, ...){
     panel.superpose(x, y, panel.groups = 'panel.bands',...)
     panel.xyplot(x, y, ...)
})
#
Il giorno mer, 20/10/2010 alle 00.05 -0700, Dieter Menne ha scritto:
version
So the problem could be somewhere else. I suspected some of this kind.

RShowDoc("NEWS", package = "lattice")
tell me that is the 0.18 version
I actually do not know how to check which version of lattice I have

For the rest

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
#
djmuseR wrote:
You are right. And use type="l" instead of r to truncate the lines to the
boxes.

Dieter
#
Il giorno mer, 20/10/2010 alle 03.05 -0700, Dennis Murphy ha scritto:
Hi all.
Thanks again to you both for the help.

It was a problem due to a lack in updating ubuntu.
After the upgrade to Lucid I entirely forgot to
update /etc/apt/sources.list with the new lines.
For some reason I only commented the old lines with hardy.

After R re-installation and upgrade.packages(), everything is working as
expected.

Otto
#
On Wed, Oct 20, 2010 at 7:53 AM, ottorino
<ottorino-luca.pantani at unifi.it> wrote:
For the record, the problem with the old lattice (0.18) was that
panel.superpose() didn't pass on 'col' to panel.groups; it passed on
'col.line' and 'col.symbol' instead. (Actually, it passed on col=NA,
which is even worse). This was OK for the default
panel.groups=panel.xyplot, but obviously a problem in cases like this.
lattice 0.19 fixes this.

One approach that will work both versions is to use a different
parameter that panel.superpose() does pass on.  'fill' seems
appropriate here, e.g.,

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

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

-Deepayan