[R] xyplot for longitudinal data

Deepayan Sarkar deepayan.sarkar at gmail.com
Wed Jul 18 01:47:59 CEST 2007


On 7/17/07, Osman Al-Radi <osman.al.radi at gmail.com> wrote:
> Dear R-help subscribers,
>
> I use xyplot to plot longitudinal data as follows:
>
> score<-runif(100,-4,5)
> group<-sample(1:4,100,rep=T)
> subject<-rep(1:25,4)
> age<-rep(runif(4,1,40),25)
> df<-data.frame(score,group,age,subject)
>
> xyplot(score~age|group, group=subject,
> panel=function(...){
> panel.loess(...,lwd=4)
> panel.superpose(...)}
> ,data=df)
>
> this produced a plot with four panels one for each group, with unique
> plotting parameters for each subject.
>
> How can I create a create a plot with a single panel where all four groups
> are superimposed using different line colors and symbols for each group, but
> preserving the longitudinal nature of the data (i.e. one line per subject).

Create an interaction and specify the colors and symbols you want
explicitly. E.g.,

spcolors <- trellis.par.get("superpose.symbol")$col

df$subject <- factor(df$subject)
df$group <- factor(df$group)

with(df,
     xyplot(score~age, groups = interaction(subject, group),
            type = "l",
            col = rep(spcolors[1:nlevels(group)], each = nlevels(subject))))


Look at ?Rows, which might help in getting code that is a bit more
general (for instance, the code above may not work when groups has
more than 7 levels).

-Deepayan



More information about the R-help mailing list