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

Duncan Mackay dulcalma at bigpond.com
Mon Aug 15 03:32:27 CEST 2016


Hi

A slightly different approach

xyplot(y~x|fac,
       groups = ifelse(age < 40,1,2),
       par.settings = list(strip.background = list(col = "transparent"),
                           superpose.symbol = list(col = c("red","blue","green")),
                           superpose.line = list(col = c("red","blue","green"))
                           ),
       auto.key = TRUE,
       panel = function(x, y, ..., groups) {
                   panel.superpose(x, y, ..., groups, panel = panel.points)
                   panel.superpose(x, y, ..., groups, panel = panel.loess)
                   panel.xyplot(x,y, type = "smooth", col = "green")
               }
)

This gives the colours to the panel function as well as the key

If you want a more elaborate key

xyplot(y~x|fac,
       groups = ifelse(age < 40,1,2),
       par.settings = list(strip.background = list(col = "transparent"),
                           superpose.symbol = list(col = c("red","blue","green")),
                           superpose.line = list(col = c("red","blue","green"))
                           ),
       key = list(text = list(labels = c("young","old","all")),
                       points = list(pch = 1, col = c("red","blue","transparent")),
                       lines = list(col = c("red","blue","green"))),
       panel = function(x, y, ..., groups) {
                   panel.superpose(x, y, ..., groups, panel = panel.points)
                   panel.superpose(x, y, ..., groups, panel = panel.loess)
                   panel.xyplot(x,y, type = "smooth", col = "green")
               }
)

Regards

Duncan

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Juan Perez via R-help
Sent: Saturday, 13 August 2016 23:32
To: Bert Gunter
Cc: r-help at r-project.org
Subject: Re: [R] Adding loess lines subsetting to each panel in lattice plot

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.
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.


  
	[[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