[R] lattice auto.key drop unused levels

Peter Ehlers ehlers at ucalgary.ca
Sun Oct 11 14:37:01 CEST 2009


Jacob Wegelin wrote:
> On Sat, Oct 10, 2009 at 8:51 PM, Peter Ehlers <ehlers at ucalgary.ca> wrote:
>> The key will show the levels of the 'groups' factor. So you will
>> have to ensure that the factor fed to groups has the levels
>> that you want displayed. ?xyplot explicitly states that
>> drop.unused.levels will NOT do that for you.
> 
> So the answer seems to be that there is no way to drop the levels
> without bypassing the "subset" argument.
> 
> The following creates the desired effect:
> 
> xyplot(yield~nitro, data=Oats[Oats$Block=="I" | Oats$Block=="II" , ] ,
> group=Block [, drop=T], auto.key=T)

This gives you the desired effect because you are manipulating
the data set used by xyplot to contain a variable 'Block' that
has 6 levels only 2 of which are represented. The way that
you're specifying the 'groups' argument then drops the unused
levels. This is equivalent to doing the following before
calling xyplot():

   data.to.plot <- subset(Oats, Block %in% c("I", "II"))

(at which point 'Block' still has 6 levels), followed by

   grouping.factor <- data.to.plot$Block[,drop = TRUE]

which will drop the unused levels of Block in grouping.factor.
Then

   xyplot(yield ~ nitro, data = data.to.plot,
     groups = grouping.factor, auto.key = TRUE)

> 
> whereas dropping levels in the "group" argument does not create the
> desired effect
> 
> xyplot(yield~nitro, subset=(Block=="I" | Block=="II"), data=Oats,
> group=Block [, drop=T], auto.key=T)

Yes, here the data set Oats still has all 72 observations and
all 6 levels of 'Block' are represented. Block[,drop = TRUE]
has no effect because there are no levels to drop.

I think the key thing is to realize that 'subset' in xyplot
does nothing to the data other than to plot only those rows
that are specified by the subset argument. It does not alter
the data.

Personally, I usually manipulate the data before passing it
to plotting functions.

Cheers,
Peter Ehlers

> 
> Jacob Wegelin
> 
>>  -Peter Ehlers
>>
>> Jacob Wegelin wrote:
>>> The following code produces a legend ("key") that mentions the unused
>>> levels of Block.
>>>
>>> library(MEMSS)
>>> xyplot(yield~nitro, subset=(Block=="I" | Block=="II"), data=Oats,
>>> group=Block, auto.key=T)
>>>
>>> and adding "drop.unused.levels=T" does not fix it. And in fact even
>>> the following does not solve the problem:
>>>
>>>  xyplot(yield~nitro, data=Oats[Oats$Block=="I" | Oats$Block=="II",],
>>> group=Block, auto.key=T)
>>>
>>> The following workaround solves it, but seems inelegant:
>>>
>>> junk<-Oats[Oats$Block=="I" | Oats$Block=="II",]
>>> junk$Block<-factor(as.character(junk$Block))
>>> xyplot(yield~nitro, group=Block, data=junk, auto.key=T)
>>>
>>> What is the elegant or "proper R thinking" way to do this? That is, I
>>> want to get a key that only mentions the levels of Block that are used
>>> in the plot.
> 
>




More information about the R-help mailing list