[R] trajectory plot (growth curve)

Kingsford Jones kingsfordjones at gmail.com
Tue Aug 24 03:20:24 CEST 2010


On Mon, Aug 23, 2010 at 6:19 PM, Dennis Murphy <djmuser at gmail.com> wrote:
>
> This is an excellent idea - the only snag might occur if someone wants
> the mean line to be thicker :)

fortunately, with your lattice solution this is easily accomplished by
passing a vector to lwd:

i <- c(1, 1, 1, 3)

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

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


but I didn't have luck trying the same with qplot:

> qplot(time, y, data = dat, group = ID, color = ID,
+     geom = c('point', 'line'), lty = i, lwd = i)
Error in data.frame(colour = c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,  :
  arguments imply differing number of rows: 18, 4

perhaps using the construct ggplot(...) + geom_line(...) would be more fruitful?

King




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



More information about the R-help mailing list