Re: [R] Lattice: groups and no groups with panel.superpose

From: baptiste auguie <ba208_at_exeter.ac.uk>
Date: Sat, 15 Nov 2008 16:59:31 +0000

Hi,

I think the following code should do what you want,

		 xyplot(yvar~year|week,data=df,layout = c(4, 5),
		        type='p',
		        groups = temp ,
		        panel = function(x, y, ...) {
		        panel.superpose(x, y, ...)
		        panel.abline(lm(y~x))
		        },
		        panel.groups = function(x, y, ..., lty) {
		                panel.xyplot(x, y, ..., lty = lty)
		                panel.abline(lm(y~x), lty=3, ...)
		        })

Hope this helps,

baptiste

On 15 Nov 2008, at 14:25, Robert Buitenwerf wrote:

>
> Dear R listers,
>
> I am trying to create a lattice plot with independent regression
> lines per group
> for each panel, plus one overall regression line per panel. I
> succeed in having
> the separate regression lines for each group per panel, but I can't
> figure out
> how to add the overall regression line over all groups. I have
> learnt a lot from
> the list, but I can't find an example for what I want to do...
>
> with the following example data (from another post):
> years<-2000:2006
> weeks<-1:20
> yr<-rep(years,rep(length(weeks)*6,length(years)))
> wk<-rep(weeks,rep(6,length(weeks)))
> temp<-rep(4:9,length(years)*length(weeks))
> yvar<-round(rnorm(length(years)*length(weeks)*6,mean=30,sd=4),0)
> xvar<-(rnorm(length(years)*length(weeks)*6)+5)/10
> df<-data.frame(year=yr,week=wk,temp=temp, yvar=yvar,
> xvar=xvar)
>
> I get the regression lines per group for each panel using:
>
> xyplot(yvar~year|week,data=df,layout = c(4, 5),
> type='p',
> groups = temp ,
> panel = panel.superpose,
> panel.groups = function(x, y, ..., lty) {
> panel.xyplot(x, y, ..., lty = lty)
> panel.abline(lm(y~x), lty=3, ...)
> })
>
> And obviously the overall regression lines with:
>
> xyplot(yvar~year|week,data=df,layout = c(4, 5),
> type='p',
> panel = function(x, y, ...) {
> panel.xyplot(x, y, ...)
> panel.abline(lm(y~x))
> })
>
> Any advice on how to "merge" the output from the two above graphs
> would be
> greatly appreciated!
>
>
> Kind regards,
>
> Robert Buitenwerf
> Ecologist
> SAEON
> South Africa
> _________________________________________________________________
> [[elided Hotmail spam]]
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> 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.


Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag



R-help_at_r-project.org mailing list
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. Received on Sat 15 Nov 2008 - 17:01:10 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 17 Nov 2008 - 09:30:25 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive