[R] lattice auto.key drop unused levels

Deepayan Sarkar deepayan.sarkar at gmail.com
Sun Oct 11 06:02:03 CEST 2009


On Sat, Oct 10, 2009 at 5: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.
>
> (Didn't Deepayan just answer something like this?)

Yes:

https://stat.ethz.ch/pipermail/r-help/2009-October/214516.html

and the Lattice book has a longer discussion (in particular section 9.2.5).

-Deepayan


>  -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.
>>
>> Thanks
>>
>> Jacob A. Wegelin
>> Assistant Professor
>> Department of Biostatistics
>> Virginia Commonwealth University
>> 730 East Broad Street Room 3006
>> P. O. Box 980032
>> Richmond VA 23298-0032
>> U.S.A.
>> E-mail: jwegelin at vcu.edu
>> URL: http://www.people.vcu.edu/~jwegelin




More information about the R-help mailing list