Skip to content
Prev 86956 / 398506 Next

indexing within panels in xyplot

On 2/21/06, Sebastian Luque <spluque at gmail.com> wrote:
The (somewhat mysterious) solution is the following:


xyplot(y ~ x | facA, groups = facB, data = toydf,
       panel = panel.superpose,
       panel.groups = function(x, y, col.line, ...) {
           panel.xyplot(x, y, ...)
           lindx <- which(y == max(y, na.rm = TRUE))
           xleft <- mean(x[lindx], na.rm = TRUE)
           fit <- lm(y[x >= xleft] ~ x[x >= xleft])
           panel.curve(coef(fit)[1] + (coef(fit)[2] * x),
                       col = col.line,
                       xleft, max(x, na.rm = TRUE))
       })

This uses the fact that panel.groups is always supplied a 'col.line'
argument (along with many others) which has been suitably calculated
for each group (see panel.superpose for how this works).

You are in fact using 'panel.superpose' directly, as that's what the
panel function defaults to when there is a 'groups' argument. However,
this will change in R-2.3.0, and to use a panel.groups argument, you
will need to explicitly specify panel=panel.superpose. Sorry for the
confusion, but I believe this to be more sensible in the bigger scheme
of things.

If you now want a different set of line colors, you can

(1) either modify the "superpose.line" parameter, or
(2) specify col.line = c('red', 'blue') etc in the xyplot call.

Hope that makes things a bit clearer.

Deepayan
--
http://www.stat.wisc.edu/~deepayan/