Hi:

This is how you could do the same in ggplot2; geom_ribbon() does the
shading. For your example, it seemed reasonable to put in reference lines,
especially since the upper limits of one confidence band abutted the lower
limits of the other in group a, so I averaged the upper and lower limits to
get 'y' values.

data <- transform(data, y = (upper + lower)/2)

library(ggplot2)

# A plot without borders around the confidence bands:
g <- ggplot(data, aes(x = x, y = y, groups = grp))
g + geom_ribbon(aes(ymin = lower, ymax = upper), fill = 'grey80') +
     geom_line(aes(colour = grp), size = 1) +
     facet_wrap(~ cond) +
     scale_colour_discrete(name = 'Group')

# Add a black border around the bands:
g + geom_ribbon(aes(ymin = lower, ymax = upper),
                               fill = 'grey80', colour = 'black') +
      geom_line(aes(colour = grp), size = 1) +
     facet_wrap(~ cond) +
     scale_colour_discrete(name = 'Group')

g is a 'ggplot' object, created by defining the input data frame and
'aesthetics' common to all plot layers that are to be added afterward.
geom_ribbon() takes two arguments as aesthetics: a lower set of values and a
corresponding upper set. (In ggplot2, aesthetics are 'mapped' to variables,
whereas other plot features outside the aes argument can be set to a single
value, such as fill color and border color. The aesthetic specific to
geom_line() is grp, to which we want to assign different colored lines and
create a legend to associate groups with colors. Faceting in ggplot2 is
analogous to conditioning in lattice - one can either facet_wrap() or
facet_grid() - I chose the former. The last line just changes the legend
title - the legend is based on a colour aesthetic, which has a discrete
number of values, hence the name.

BTW, if you'd prefer a white background, add  + theme_bw() to the end of
either code chunk above.

HTH,
Dennis


On Tue, Oct 19, 2010 at 9:29 AM, ottorino <ottorino-luca.pantani@unifi.it>wrote:

> 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
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]

