[R] Adding loess lines subsetting to each panel in lattice plot

Bert Gunter bgunter.4567 at gmail.com
Sun Aug 14 13:29:19 CEST 2016


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:
> Inline.
>
> -- Bert
>
>
>
> On Sat, Aug 13, 2016 at 6:31 AM, Juan Perez <beire55 at yahoo.es> wrote:
>> Thanks Bert
>>
>> I am not sure what you meant by reading though.
>>
>> I managed to sort this out, although ideally would exist a better solution.
>> I have created a column in my dataset, and given two codes depending whether
>> older or younger than 40. Afterwards I have applied
>>
>> xyplot(MOE~Age|Species, groups=Old,
>>        panel= function(x,y,...){  ## custom panel function to add an overall
>> loess line
>>          panel.superpose(x,y,...)
>>          panel.loess(x,y,col="black",lwd=2,...)
>>        },
>>        panel.groups = function(x,y,...){
>>          panel.xyplot(x,y,...)
>>          panel.loess(x,y,...)
>>        })
>>
>> This gives a loess line for data younger than 40, another for the ones
>> older, and a third one for all the data. I hope this helps to someone else.
>> Unfortunately, the dataset needs to be modified everytime we want to make a
>> new or different subset.
>
> Not true. Creating a grouping factor and using panel.groups is
> certainly one way to do it, but it's not the only way. You could
> alternatively create the groups in the panel function by using the
> 'subscripts' argument, something like:
>
> x <- runif(100)
> y <- rnorm(100)
> fac <- rep(LETTERS[1:4], e= 25)
> age <- runif(100,20,60)
> pnl <- function(x,y,age, subscripts=TRUE,...){
>   age <- age[subscripts]
>   old <- age<=40
>   panel.xyplot(x,y,...)
>   panel.loess(x,y,col.line = "black")
>   panel.loess(x[old],y[old],col.line = "red")
>   panel.loess(x[!old],y[!old],col.line = "green")
> }
>
> xyplot(y~x| fac, panel = pnl, age=age,lay=c(2,2))
>
> Cheers,
> Bert
>
>>
>> Best regards
>>
>>
>> El Viernes 12 de agosto de 2016 21:10, Bert Gunter <bgunter.4567 at gmail.com>
>> escribió:
>>
>>
>> Try reading ?panel.loess. There is no "subset" argument, so it is of
>> course ignored.
>>
>> -- 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 Fri, Aug 12, 2016 at 9:10 AM, Juan Perez via R-help
>> <r-help at r-project.org> wrote:
>>> Hello, I've created an xyplot and I want to add a loess line for x (Age)
>>> <=40 and another for values >40. In a way it is similar to this
>>> https://stat.ethz.ch/pipermail/r-help/2009-May/390502.html but still not
>>> succcessful.
>>> This is my try:
>>>
>>> xyplot(MOE~Age|Species, groups=Site,
>>>        panel = function(x, y, groups=groups,...) {
>>>        panel.xyplot(x, y, groups=groups,...)
>>>        panel.loess(x,y,subset = x <= 40, col="black")
>>> panel.loess(x,y,subset = x >40, col="red")
>>>              })
>>> When I run the code it "works" but it plots the loess line for all the
>>> data, without subsetting.Any suggestion?
>>> Thank you
>>>
>>>        [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>
>>



More information about the R-help mailing list