Skip to content

Adding loess lines subsetting to each panel in lattice plot

1 message · Bert Gunter

#
Juan:

What I gave you previously was correct, but not "nice". The subscripts
= TRUE argument should have been in the xyplot() call, not the pnl
function. However, it is actually not explicitly needed there either,
because it will default to TRUE if the panel function has a subscripts
argument, which it does (and did -- that's why it worked before). Here
is the more sensible version (cc'ed to the list, which I fortunately
failed to do before):

## set up example
x <- runif(100)
 y <- rnorm(100)
 fac <- rep(LETTERS[1:4], e= 25)
 age <- sample(1:2,50,rep=TRUE)
 x <- runif(100)
 y <- rnorm(100)
 fac <- rep(LETTERS[1:4], e= 25)
 age <- runif(100,20,60)

#panel function
 pnl <- function(x, y, age, subscripts, ...){
     age <- age[subscripts]
     old <- age<=40
     panel.xyplot(x,y,...)
     panel.loess(x,y,col.line = "green")
     panel.loess(x[old],y[old],col.line = "red")
     panel.loess(x[!old],y[!old],col.line = "blue")
 }

 xyplot(y~x|fac, age=age,  panel=pnl, lay=c(2,2))


Note that my prior comments about moviing the creation of the groups
to the panel function instead of the data still hold, of course.

Cheers,
Bert
Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Sat, Aug 13, 2016 at 7:49 AM, Bert Gunter <bgunter.4567 at gmail.com> wrote: