[R] Auto.key colors maintained when subsetting

From: David Afshartous <dafshartous_at_med.miami.edu>
Date: Wed, 09 Jul 2008 10:50:17 -0400

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)
) ) )

R-help_at_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. Received on Wed 09 Jul 2008 - 14:50:39 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 09 Jul 2008 - 18:31:35 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive