[R] Lattice: panel superpose with groups

hadley wickham h.wickham at gmail.com
Wed Sep 5 02:05:48 CEST 2007


Hi Michael,

It's not lattice, but you can easily do this with ggplot2:

install.packages("ggplot2")
library(ggplot2)
qplot(year, yvar, data=df, facets = . ~ week, colour=factor(temp),
geom="line") +
stat_summary(aes(group=1), geom="line", fun="mean", size=2)

Although you don't (currently) get the nice tabular layout of the
panels like in lattice.  You can find out more about ggplot2 at
http://had.co.nz/ggplot2

Hadley

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


-- 
http://had.co.nz/



More information about the R-help mailing list