Timothy W. Hilton
2015-Jan-27 18:19 UTC
[R] panel.xyplot and panel.loess using two different groupings
Hello, I have a dataset consisting of four variables: species (factor, five levels), C3C4 (factor, two levels), and numeric variables PAR and LRU. I wish to produce a scatter plot of PAR vs LRU where (1) each species has a unique symbol and color (2) there is an overlaid loess line that is calculated for each C3C4 level. The following code produces a nice plot where each species has its own loess curve, but I'm struggling for the syntax to produce a single loess curve for all the points in each of the two plots: xy <- xyplot(LRU~PAR|C3C4, data=light_data, groups=species, panel=panel.superpose, auto.key=TRUE, col.line=pal, par.settings=mytheme, panel.groups=function(x, y, ...){ panel.xyplot(x, y, ...) panel.loess(x, y, ...)}) Any help much appreciated. Thanks! Tim -- Timothy W. Hilton Assistant Project Scientist Sierra Nevada Research Institute University of California, Merced thilton at ucmerced.edu
Bert Gunter
2015-Jan-27 19:13 UTC
[R] panel.xyplot and panel.loess using two different groupings
Learn to use custom panel functions to give you the flexibility and features you require. In this case, you want something like: xyplot(LRU~PAR|C3C4, groups = species, panel= function(x,y,...){ ## custom panel function to add an overall loess line panel.superpose(x,y,...) panel.loess(x,y,col="darkblue",...) }, panel.groups = function(x,y,...){ panel.xyplot(x,y,...) panel.loess(x,y,...) } ## plus any other key and plot options ) Note that this could fail if groups have too few points to fit a loess curve, so you might want to add checks in the panel.groups function for this. HTH. -- Bert Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." Clifford Stoll On Tue, Jan 27, 2015 at 10:19 AM, Timothy W. Hilton <thilton at ucmerced.edu> wrote:> Hello, > > I have a dataset consisting of four variables: species (factor, five > levels), C3C4 (factor, two levels), and numeric variables PAR and LRU. > I wish to produce a scatter plot of PAR vs LRU where (1) each species > has a unique symbol and color (2) there is an overlaid loess line that > is calculated for each C3C4 level. The following code produces a nice > plot where each species has its own loess curve, but I'm struggling for > the syntax to produce a single loess curve for all the points in each of > the two plots: > > xy <- xyplot(LRU~PAR|C3C4, data=light_data, groups=species, > panel=panel.superpose, > auto.key=TRUE, > col.line=pal, > par.settings=mytheme, > panel.groups=function(x, y, ...){ > panel.xyplot(x, y, ...) > panel.loess(x, y, ...)}) > > Any help much appreciated. Thanks! > Tim > > -- > > Timothy W. Hilton > Assistant Project Scientist > Sierra Nevada Research Institute > University of California, Merced > thilton at ucmerced.edu > > ______________________________________________ > 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.
Timothy W. Hilton
2015-Jan-27 21:55 UTC
[R] panel.xyplot and panel.loess using two different groupings
Thanks, Bert, for the reply. This is very helpful. I have to admit I've read the docs for the panel and panel.groups arguments before and gotten myself pretty confused. Your small example is very helpful. One more question for the list... Bert's panel.loess col='darkblue' argument is being overridden by my call to par.settings=mytheme, which I've defined like this (using the RColorBrewer library): pal <- brewer.pal(length(levels(light_data[['species']])), 'Dark2') mytheme <- standard.theme("pdf") mytheme[['superpose.symbol']][['pch']] <- c(15,16,17,3,4) mytheme[['superpose.symbol']][['col']] <- pal I've not been able to figure out which trellis.par.get() setting controls the loess line colors, so I've not been able to set those in the theme. Similarly, I've not been able to pass a line color argument to loess that doesn't get clobbered by the theme. Is this possible? Thanks, -Tim On Tue, Jan 2015, 27 at 11:13:51AM -0800, Bert Gunter wrote:> Learn to use custom panel functions to give you the flexibility and > features you require. > > In this case, you want something like: > > xyplot(LRU~PAR|C3C4, groups = species, > panel= function(x,y,...){ ## custom panel function to add an > overall loess line > panel.superpose(x,y,...) > panel.loess(x,y,col="darkblue",...) > }, > panel.groups = function(x,y,...){ > panel.xyplot(x,y,...) > panel.loess(x,y,...) > } > ## plus any other key and plot options > ) > > > Note that this could fail if groups have too few points to fit a loess > curve, so you might want to add checks in the panel.groups function > for this. > > > HTH. > > > -- Bert > > Bert Gunter > Genentech Nonclinical Biostatistics > (650) 467-7374 > > "Data is not information. Information is not knowledge. And knowledge > is certainly not wisdom." > Clifford Stoll > > > > > On Tue, Jan 27, 2015 at 10:19 AM, Timothy W. Hilton > <thilton at ucmerced.edu> wrote: > > Hello, > > > > I have a dataset consisting of four variables: species (factor, five > > levels), C3C4 (factor, two levels), and numeric variables PAR and LRU. > > I wish to produce a scatter plot of PAR vs LRU where (1) each species > > has a unique symbol and color (2) there is an overlaid loess line that > > is calculated for each C3C4 level. The following code produces a nice > > plot where each species has its own loess curve, but I'm struggling for > > the syntax to produce a single loess curve for all the points in each of > > the two plots: > > > > xy <- xyplot(LRU~PAR|C3C4, data=light_data, groups=species, > > panel=panel.superpose, > > auto.key=TRUE, > > col.line=pal, > > par.settings=mytheme, > > panel.groups=function(x, y, ...){ > > panel.xyplot(x, y, ...) > > panel.loess(x, y, ...)}) > > > > Any help much appreciated. Thanks! > > Tim > > > > -- > > > > Timothy W. Hilton > > Assistant Project Scientist > > Sierra Nevada Research Institute > > University of California, Merced > > thilton at ucmerced.edu > > > > ______________________________________________ > > 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.