[R] subsetting key on qqmath

Deepayan Sarkar deepayan.sarkar at gmail.com
Sat Oct 10 11:11:19 CEST 2009

On Thu, Oct 8, 2009 at 8:11 PM, John Field <JohnField at ozemail.com.au> wrote:
> Dear R list,
> The code below puts qq-plots for two of three groups on the one plot.
>  However the legend includes all three groups, ie the auto.key ignores the
> subset instruction.  Is there an easy way to get around this, so that only
> those groups plotted are included in the legend?
> y1<-rnorm(100); y2<-rnorm(100)+1; y3<-rnorm(100)+2; y<-c(y1,y2,y3)
> fact=factor(c(rep(1,100),rep(2,100),rep(3,100)))
> require(lattice)
> qqmath(~y,groups=fact,auto.key=list(corner=c(0,1)),subset=fact!=2)

The subsetting of groups happens independently in each panel. As
there's no guarantee in general that the subset will have the same
non-empty set of levels in each panel, the only solution (with a
common legend) is to keep all the levels.

To drop unused levels in groups, you need to subset your data
beforehand. For example,

df <- data.frame(y=c(y1,y2,y3),
qqmath(~y, data=subset(df, fact!=2),


More information about the R-help mailing list