[R] 2 bwplots - different colors

Deepayan Sarkar deepayan at stat.wisc.edu
Mon Feb 16 19:17:43 CET 2004


On Monday 16 February 2004 10:37, Martina Pavlicova wrote:

> Hi all,
>
> I would like to draw one picture which would show two
> different types of boxplots using the same axes (kind of
> on top of each other). However, I would like to plot each
> boxplot using a different color or different shading
> inside the box, so they could be better distinquished
> from each other... Could you help me?
>
> Here is an example of the plot I have so far. I was only
> able to change the color the median-dot (to 'red').

Your code doesn't work for me as it is.

bwplot isn't really designed for grouped displays. Why don't 
you plot them in different panels ? I don't see any point 
in superposing them. For example,


library(lattice)
foo1 <- rnorm(600)
foo2 <- rchisq(600, 3)
bwplot(foo1 + foo2 ~ gl(20, 30), allow.m = T, outer = T)



If you really want to superimpose them, I would suggest 
using the following approach:



bwplot(foo1 + foo2 ~ gl(20, 30), allow.m = T, outer = F,
       panel =
       function(x, y, subscripts, groups, ...) {
           opar <- trellis.par.get()
           x <- as.numeric(x)
           y <- as.numeric(y)

           settings <- list()
           settings[[1]] <- 
               list(box.rectangle = list(col = "cyan"),
                    box.umbrella = list(col = "cyan"),
                    plot.symbol = list(col = "cyan"),
                    box.dot = list(col = "blue"))

           settings[[2]] <- 
               list(box.rectangle = list(col = "pink"),
                    box.umbrella = list(col = "pink"),
                    plot.symbol = list(col = "pink"),
                    box.dot = list(col = "red"))

           vals <- levels(groups)
           for (i in 1:2)
           {
               lset(settings[[i]])
               id <- groups[subscripts] == vals[i]
               panel.bwplot(x = x[id], y = y[id], ...)
               lset(opar)
           }
       })



Note that there are too many graphical parameters 
controlling boxplots to be included in panel.bwplot, and 
you need to modify the global settings to get anything 
useful. The relevant parameters that you may want to modify 
are given by

trellis.par.get("box.rectangle")
trellis.par.get("box.umbrella")
trellis.par.get("box.dot")
trellis.par.get("plot.symbol")


Hope that helps,

Deepayan




More information about the R-help mailing list