[R] lattice auto.key drop unused levels

Jacob Wegelin jacob.wegelin at gmail.com
Sun Oct 11 04:28:15 CEST 2009


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)

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)

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