[R] xyplot plot with groups
array chip
arrayprofile at yahoo.com
Sat Aug 21 00:07:59 CEST 2010
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
More information about the R-help
mailing list