Hi:

On Mon, Aug 23, 2010 at 4:19 PM, Kingsford Jones
<kingsfordjones@gmail.com>wrote:

> Hi Lei,
>
> Hope you don't mind I'm moving this back to the list in case others
> may benefit.  Answers below...
>
> On Mon, Aug 23, 2010 at 3:37 PM, Lei Liu <liulei@virginia.edu> wrote:
> > Hi Kingsford,
> >
> > Thanks a lot! I got some help from my colleague by using the following
> code:
> >
> >  xyplot(y~month,group=id, type="l"), the same as you suggested. It worked
> > fine.
> >
> > However, when I tried to add an additional line for the mean at each time
> > point by the following code:
> >
> >  y.mean=aggregate(y, by=list(time), FUN=mean)[, 2]
> >  uniq.time=sort(unique(time))
> >
> >  lines(uniq.time, y.mean, type="l", lty=1, lw=2)
> >
> > I find the line of mean does not overlap well with the trajectory plot!!!
> It
> > seems to me that "lines" statement does work well under "xyplot"! I tried
> > different strategies, e.g., add xlim and ylim in both xyplot and lines
> > statements, but still the problem exists. I also tried the ggplot2
> package
> > and it had the same problem. Any help here? Thanks!
>
> Both lattice and ggplot2 use grid graphics which is a different beast
> from the base graphics.  I don't believe the lines function has
> methods to add to grid plots.  There are many approaches you could
> take here.  The first thing that comes to my mind is to add another
> subjects (named 'mean' below) whose values are the observed average
> within time points:
>

This is an excellent idea - the only snag might occur if someone wants
the mean line to be thicker :)  Having said that, it's usually easier to
'fix' the
problem externally in the data rather than to fiddle with graphics commands.


> #the original data (no replicates within time points)
> dat <- structure(list(ID = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 2L),
>  .Label = c("1", "2"), class = "factor"),
>  time = c(1, 2, 3, 1.5, 4, 5.5, 6),
>  y = c(1.4, 2, 2.5, 2.3, 4.5, 1.6, 2)),
>  .Names = c("ID", "time", "y"),
>  row.names = c(NA, -7L), class = "data.frame")
>
> #adding another subject to introduce replicates
> id3 <- data.frame(ID=as.factor(rep(3, 4)),time = c(1, 1.5, 2, 5.5),
>                 y = c(1, 2.2, 3, 2))
> dat <- rbind(dat, id3)
> mean.y <- aggregate(formula = y ~ time, data = dat, FUN = mean)
> mean.y <- cbind(ID = as.factor('mean'), mean.y)
> dat <- rbind(dat, mean.y)
> dat
> library(ggplot2)
> qplot(time, y, data=dat, group = ID, color = ID, geom = c('point', 'line'))
>

 A lattice version with a legend is:

mykey <- list(space = 'right',
              title = 'ID',
              cex.title = 1.2,
              text = list(levels(dat$ID), cex = 0.8),
              lines = list(lty = 1, col = 1:4))

xyplot(y ~ time, data = dat, lty = 1, col.lines = 1:4, col = 1:4,
     groups = ID, type = c('g', 'p', 'l'), key = mykey)

Defining the key externally modularizes the problem, lets one define
the features one wants to contain, and simplifies the high-level
xyplot() call.

There is a type = 'a' (shorthand for panel.average()) that can be
used to good effect in xyplot(), but it creates 'holes' where missing
data reside, so taking care of the problem externally at the data
level is much cleaner.

HTH,
Dennis


> best,
>
> Kingsford Jones
>
> ______________________________________________
> 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]]

