Re: [R] indexing within panels in xyplot

From: Frede Aakmann Tøgersen <FredeA.Togersen_at_agrsci.dk>
Date: Tue 21 Feb 2006 - 18:57:11 EST

Based on your two first sentences I think the solution is to use

xyplot(y ~ x | facA, groups = facB, data = toydf,type=c("p","r"))

Try it and see if this is what you want.

Best regards

Frede Aakmann Tøgersen
Scientist

Danish Institute of Agricultural Sciences Research Centre Foulum
Dept. of Genetics and Biotechnology
Blichers Allé 20, P.O. BOX 50
DK-8830 Tjele

Phone: +45 8999 1900
Direct: +45 8999 1878

E-mail: FredeA.Togersen@agrsci.dk
Web: http://www.agrsci.org

This email may contain information that is confidential. Any use or publication of this email without written permission from DIAS is not allowed. If you are not the intended recipient, please notify DIAS immediately and delete this email.  

> -----Oprindelig meddelelse-----
> Fra: r-help-bounces@stat.math.ethz.ch
> [mailto:r-help-bounces@stat.math.ethz.ch] På vegne af Sebastian Luque
> Sendt: 21. februar 2006 08:20
> Til: r-help@stat.math.ethz.ch
> Emne: [R] indexing within panels in xyplot
>
> 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.
>
> Thanks,
>
> --
> Sebastian P. Luque
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
>



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue Feb 21 19:01:24 2006

This archive was generated by hypermail 2.1.8 : Wed 22 Feb 2006 - 09:27:42 EST