I'm trying to understand how to plot individual growth curve trajectories, with the overall mean trajectory superimposed (preferably in a slightly thicker line, maybe in black) over the individual trajectories. Using the sleepstudy data in lme4, here is the code I have so far: library(lme4) library(lattice) xyplot(Reaction ~ Days, data = sleepstudy, group = Subject, type = 'l') This plot produces the individual growth curves nicely, but I'd like to be able to plot the mean for each day (averaged over subjects) on top of this graph. Many thanks in advance for any help/suggestions. John -- View this message in context: http://r.789695.n4.nabble.com/Mean-and-individual-growth-curve-trajectories-tp3021672p3021672.html Sent from the R help mailing list archive at Nabble.com.
jlwoodard <john.woodard <at> wayne.edu> writes:> > > I'm trying to understand how to plot individual growth curve trajectories, > with the overall mean trajectory superimposed (preferably in a slightly > thicker line, maybe in black) over the individual trajectories. Using the > sleepstudy data in lme4, here is the code I have so far: > > library(lme4) > library(lattice) > xyplot(Reaction ~ Days, data = sleepstudy, group = Subject, type = 'l') > > This plot produces the individual growth curves nicely, but I'd like to be > able to plot the mean for each day (averaged over subjects) on top of this > graph.Is this what you want? xyplot(Reaction ~ Days, data = sleepstudy, group = Subject, type = 'l', panel=function(...){ panel.xyplot(...) panel.average(...,fun=mean,horizontal=FALSE,col='red',lwd=3) } ) and have you considered: xyplot(Reaction ~ Days, data = sleepstudy, group = Subject, type = 'l', panel=function(...){ panel.xyplot(...) panel.loess(...,fun=mean,horizontal=FALSE,col='red',lwd=3) } ) for a smoother curve? Hope it helps, Michael Bibo Queensland Health
Thank you so much, Michael. This solution is just what I was looking for. Many thanks! John -- View this message in context: http://r.789695.n4.nabble.com/Mean-and-individual-growth-curve-trajectories-tp3021672p3021746.html Sent from the R help mailing list archive at Nabble.com.
Hi: Here's another version of the plot using ggplot2: g <- ggplot(sleepstudy, aes(x = Days, y = Reaction, group = Subject, colour = Subject)) g + geom_line(size = 1) + geom_smooth(aes(group = 1), size = 2) + theme_bw() To get rid of the legend, if you so desire, use g + geom_line(size = 1) + geom_smooth(aes(group = 1), size = 2) + theme_bw() + opts(legend.position = 'none') By default, geom_smooth() uses a loess curve for smoothing; use method 'lm' outside the aes() call to get a least squares line instead. If you don't want the useful confidence limits on the smooth :), add se = FALSE to the geom_smooth() argument list. I tend to like size = 1 for lines, but the default size produces thinner lines, if that's what you prefer. HTH, Dennis On Sun, Oct 31, 2010 at 7:01 PM, jlwoodard <john.woodard@wayne.edu> wrote:> > I'm trying to understand how to plot individual growth curve trajectories, > with the overall mean trajectory superimposed (preferably in a slightly > thicker line, maybe in black) over the individual trajectories. Using the > sleepstudy data in lme4, here is the code I have so far: > > library(lme4) > library(lattice) > xyplot(Reaction ~ Days, data = sleepstudy, group = Subject, type = 'l') > > This plot produces the individual growth curves nicely, but I'd like to be > able to plot the mean for each day (averaged over subjects) on top of this > graph. > > Many thanks in advance for any help/suggestions. > > John > > -- > View this message in context: > http://r.789695.n4.nabble.com/Mean-and-individual-growth-curve-trajectories-tp3021672p3021672.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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]]