[R] Lattice: panel superpose with groups
Folkes, Michael
FolkesM at pac.dfo-mpo.gc.ca
Wed Sep 5 02:40:23 CEST 2007
Thank you again Deepayan. I was failing to grasp that I could use
panel.groups as a function. But additionally it's still not intuitive
to me where and when I should use "..." to pass arguments on.
Additionally, as to why the panel.group function needs to pass the 'lty'
argument isn't terribly clear to me! Perhaps it will become clear with
time.
I greatly appreciate your patience and assistance.
Thanks all,
Michael Folkes
-----Original Message-----
From: Deepayan Sarkar [mailto:deepayan.sarkar at gmail.com]
Sent: September 4, 2007 5:11 PM
To: Folkes, Michael
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] Lattice: panel superpose with groups
On 9/4/07, Folkes, Michael <FolkesM at pac.dfo-mpo.gc.ca> wrote:
> The example code below allows the plotting of three different groups
> per panel. I can't fathom how to write the panel function to add an
> additional line for each group, which in this case is just the mean Y
> value for each group within each panel. (i.e. there'd be six lines
> per panel.) Spent all day working on it and searching the archives to
> no avail! Yikes. Any help would be greatly appreciated!
xyplot(yvar~year|week2,data=df,layout = c(4, 5), as.table=TRUE,
type='l',
groups = temp ,
panel = panel.superpose,
panel.groups = function(x, y, ..., lty) {
panel.xyplot(x, y, ..., lty = lty)
#panel.lines(x, rep(mean(y),length(x)), lty=3, ...) # or
panel.abline(h = mean(y), lty=3, ...)
})
(see ?panel.superpose for explanation)
-Deepayan
> Michael Folkes
>
> #________________________
> #This builds fake dataset
>
> 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)
> #________________________
>
> library(lattice)
> df$year2<-as.factor(df$year)
> df$week2<-as.factor(df$week)
> df<-df[df$temp %in% c(5,7,9),] xyplot(yvar~year|week2,data=df,layout =
> c(4, 5), as.table=TRUE,
> type='l',
> groups=temp ,
> panel = function(x, y,groups, ...) {
> panel.superpose(x,y,groups,...)
> panel.xyplot(x,rep(mean(y),length(x)),type='l',lty=3)
#<- only generates the panel mean
> }
> )
>
> _______________________________________________________
> Michael Folkes
> Salmon Stock Assessment
> Canadian Dept. of Fisheries & Oceans
> Pacific Biological Station
> 3190 Hammond Bay Rd.
> Nanaimo, B.C., Canada
> V9T-6N7
> Ph (250) 756-7264 Fax (250) 756-7053 folkesm at pac.dfo-mpo.gc.ca
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>
More information about the R-help
mailing list