What a simple way to do what I want to do! Thanks. But if there is missing data in variable "y", then the averaged lines is broken where the missing data is present. For example:
dat$y[c(10,185,200,400,450)]<-NA then using type='a' option will result in broken lines. xyplot(y~day|sex, groups=trt, data=dat, type=c('p','g', 'a')) I was able to solve this using panel.superpose() and panel.average(). But it's too much syntax. Is there a simpler way to do this? xyplot(y~day|sex, groups=trt, data=dat, type=c('p','g'), panel=function(...) {panel.xyplot(...); panel.superpose(..., col=c(1:3), panel.groups=function(...) panel.average(..., fun=function(x) mean(x,na.rm=T),horizontal=F))}) Thanks John ________________________________ From: Dennis Murphy <djmu...@gmail.com> Cc: r-help@r-project.org Sent: Fri, August 20, 2010 3:25:06 PM Subject: Re: [R] xyplot plot with groups Hi: Like this? xyplot(y~day|sex, groups=trt, data=dat, type=c('p','g', 'a')) The 'a' stands for average line, or 'connect the averages at different x values'. By using the groups = trt argument, you get one line per treatment group in each panel. A complete list of valid types is given on p. 75 of the Lattice book (Sarkar, 2008). Related types are: 'l' : join points by lines 'r': least squares line 'smooth': loess smooth HTH, Dennis Hi, I am a beginner of xyplot() (or lattice package). On one hand, I immediately >realized it's a very powerful utility. On the other hand, there are too many >things for me to learn. Still haven't figure out a generalization of the syntax >and usage under many different circumstances. > >Let me give an example dataset: > > >xxx<-cbind(expand.grid(day=0:7,trt=c('A','B','C'),sex=c('M','F')),subject=rep(1:60,each=8)) > > >xxx<-xxx[sort.list(xxx$trt),] >y<-c(rnorm(160,15),rnorm(160,10),rnorm(160,5)) > >dat<-cbind(y=y,xxx) > >str(dat) >'data.frame': 480 obs. of 5 variables: > $ y : num 13.4 15.3 14.4 16.2 15.1 ... > $ day : int 0 1 2 3 4 5 6 7 0 1 ... > $ trt : Factor w/ 3 levels "A","B","C": 1 1 1 1 1 1 1 1 1 1 ... > $ sex : Factor w/ 2 levels "M","F": 1 1 1 1 1 1 1 1 2 2 ... > $ subject: int 1 1 1 1 1 1 1 1 4 4 ... > >So this is a typical longitudinal dataset: each subject has measurements for 8 >days (0 to 7), there are 3 treatments (A, B and C), and sex factor (M and F). I >would like to have 2 plots, one for each sex. Within each plot, I want to plot 3 >averaged lines (average of y for each day), one for each treatment group. How >can I do this? > >I tried the following using panel.average(), but it seems that the groups >information was not passed automatically to panel.average(). > > >xyplot(y~day|sex, groups=trt, data=dat, type=c('p','g'), panel=function(...) >{panel.xyplot(...); panel.average(..., fun=function(x) >mean(x,na.rm=T),horizontal=F,col=1:3)}) > >Appreciate if anyone can help. > >John > >______________________________________________ >R-help@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. > [[alternative HTML version deleted]] ______________________________________________ R-help@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.