[R] Auto.key colors maintained when subsetting

Deepayan Sarkar deepayan.sarkar at gmail.com
Wed Jul 9 19:07:24 CEST 2008

On 7/9/08, David Afshartous <dafshartous at med.miami.edu> wrote:
>  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.

Well, the ideal solution would have been for auto.key to magically
omit the levels of 'groups' that are omitted by the application of
'subset', but there is not enough information available to it for this
to happen. One option is to

1. subset the data beforehand, and drop unsed levels with Group[drop=TRUE]

2. supply colors, etc. explicitly through 'par.settings'.

The other option is to construct your own key (i.e., use the 'key'
argument, not 'auto.key', to specify the legend). It should be simple
to write a function that constructs a suitable 'key' argument given
the levels that are to be omitted/retained.


More information about the R-help mailing list