[R] Lattice: panel superpose with groups

Deepayan Sarkar deepayan.sarkar at gmail.com
Wed Sep 5 02:11:19 CEST 2007


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