[R] indexing within panels in xyplot

Deepayan Sarkar deepayan.sarkar at gmail.com
Tue Feb 21 17:06:46 CET 2006


On 2/21/06, Sebastian Luque <spluque at gmail.com> wrote:
> Dear R-helpers,
>
> I need to show a linear fit through a subset of the data within each
> combination of levels of two factors.  So I prepared an xyplot with
> different panels for each level of one of the factors, and different
> symbols within each panel for the levels of the second factor.  My problem
> is selecting the subset of each combination through which the line should
> be fit for subsequent plotting.  This hopefully shows the idea:
>
>
> ---<---------------cut here---------------start-------------->---
> toydf <- expand.grid(1:100, c("A", "B"),
>                      c("pop1", "pop2", "pop3", "pop4", "pop5"))
> toydf <- data.frame(facA = toydf[[3]], facB = toydf[[2]],
>                     x = toydf[[1]], y = rnorm(1000))
>
> xyplot(y ~ x | facA, groups = facB, data = toydf,
>        panel.groups = function(x, y, subscripts, ...) {
>          panel.xyplot(x, y, ...)
>          lindx <- which(y[subscripts] == max(y[subscripts], na.rm = TRUE))
>          xleft <- mean(x[lindx], na.rm = TRUE)
>          fit <- lm(y[x >= xleft] ~ x[x >= xleft])
>          panel.abline(fit)
>        })
> ---<---------------cut here---------------end---------------->---
>
> i.e. the left limit for fitting the line is defined by the mean of x
> values where y is equal to the maximum y values, *within* each combination
> of levels of both factors.  The above is giving me:
>
> Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :
> 	0 (non-NA) cases
> In addition: Warning message:
> no finite arguments to max; returning -Inf
>
> which shows I'm not understanding how the 'subscripts' argument works.
> I'd appreciate some pointers on what I'm doing wrong, as I haven't been
> able to find help in the help pages and List archives.

Well, there are exceptions to this rule, but generally x and y, when
they are passed on to the panel function, are _already_ subsetted, so
x[subscripts] makes absolutely no sense. Note how your panel function
calls

panel.xyplot(x, y, ...)

without referring to subscripts at all. The subscripts argument is
there for other variables (e.g. if you were drawing confidence
intervals, and had a separate vector in your data specifying the
interval lengths). In your case, there are no other variables
involved, so just get rid of the subscripts.

Deepayan




More information about the R-help mailing list