[R] Lattice:can't subset in panel function using other variables

Deepayan Sarkar deepayan.sarkar at gmail.com
Sat Sep 1 02:18:43 CEST 2007


On 8/31/07, Folkes, Michael <FolkesM at pac.dfo-mpo.gc.ca> wrote:
> Thanks Deepayan for your response.
> The first subset you suggest was just a test for me and not what I
> wanted.
> I can't do your second suggested subset action as I wish to plot all the
> panel data, but then add a coloured datapoint for just one year (see
> example code).
> I think I have found my problem but don't know how to solve it.
> The subscripts of data going into each panel are almost always the same
> length, except maybe one or two panels have 1 less datapoint.
> I've attached a script that builds a quick dataset and plots what I was
> aiming for.  It works great.  If you then remove one line of data from
> the DF (using "df<-df[-1,]" in the script), the plotting goes awry.
>
> Any suggestions about dealing with unequal data lengths for panel
> function subsetting?

If your goal is to highlight one particular year, why not use something like

xyplot(yvar~xvar|week2,data=df,layout = c(4, 5), as.table=TRUE,
       groups = (year == 2005), col = 1, pch = c(1, 16))

? Your code doesn't work because you don't seem to understand what
'subscripts' is supposed to be (either that, or you are confusing
yourself with multiple indices). Here's a version with the correct
usage:

xyplot(yvar~xvar|week2,data=df,layout = c(4, 5), as.table=TRUE,
       panel = function(x, y, subscripts, ...) {
           highlight <- (df$year == 2005)
           highlight.panel <- highlight[subscripts]
           panel.xyplot(x, y, type='p', col=1, cex=.5)
           panel.xyplot(x[highlight.panel], y[highlight.panel],
                        type='p', pch=1, col=3, cex=1.5)
       })

-Deepayan



More information about the R-help mailing list