[R] Auto.key colors maintained when subsetting

David Afshartous dafshartous at med.miami.edu
Wed Jul 9 16:50:17 CEST 2008



All,

I'm plotting points and lines for various groups.
I'd like subsequent plots done on subsets to maintain the color assignments
from the original plot.  This works fine, but the key for the subset doesn't
maintain the correspondence.  One solution is to reprint the entire key, but
this is undesirable since then the key has more elements than the groups in
the plot.  Reproducible example below.

Cheers,
David




dat = data.frame(
Y = c(rnorm(4,0), rnorm(4,2), rnorm(4,6), rnorm(4,8)),
Group = factor(c(rep("A", 4), rep("B", 4), rep("C", 4), rep("D", 4))),
Hour = rep(c(1:4), 4)
)


## plots colors and key correctly:
xyplot(Y ~ Hour, 
        data = dat, pch = 16,
        groups=Group,
        type="b",
        auto.key = list(space = "top",  text = levels(Group), points =
FALSE, lines = TRUE, columns=4),
        par.settings = list(superpose.line = list(lty = c(1,2,3,4)
) ) )  



dev.new()

## does not plot colors and key correctly:
xyplot(Y ~ Hour,
        data = dat, pch = 16,
        subset = (Group != "A"),
        groups=Group,
        type="b",
        auto.key = list(space = "top",  text = levels(Group)[2:4], points =
FALSE, lines = TRUE, columns=3),
        par.settings = list(superpose.line = list(lty = c(1,2,3,4)
) ) )



More information about the R-help mailing list