dadrivr
2011-Feb-20 20:59 UTC
[R] Plotting individual trajectories from individual growth model
Hi all, I am trying to plot the fitted trajectories for each individual from an individual growth model (fit with a linear mixed effects model in lme). How can I plot each person's trajectory in the *same* panel, along with the mean-level trajectory? Below is an image of a plot similar to what I'm trying to create (from: http://jpepsy.oxfordjournals.org/content/31/10/1002/F6.large.jpg): http://r.789695.n4.nabble.com/file/n3315494/fitted_trajectories.jpg Thanks so much for your help! -- View this message in context: http://r.789695.n4.nabble.com/Plotting-individual-trajectories-from-individual-growth-model-tp3315494p3315494.html Sent from the R help mailing list archive at Nabble.com.
Dennis Murphy
2011-Feb-21 06:20 UTC
[R] Plotting individual trajectories from individual growth model
Hi: These are sometimes called 'spaghetti plots'; here is a variation on an example in the ggplot2 book by Hadley Wickham using the Oxboys data from package nlme: library(ggplot2) data('Oxboys', package = 'nlme') g <- ggplot(Oxboys, aes(x = age, y = height)) g + geom_line(aes(group = Subject)) + geom_smooth(aes(group = 1), colour = 'red', size = 1) If you're not familiar with the package... * The first line that defines the object g sets up the 'base plot'; its core 'aesthetics', or graphics elements, if you will, are the x-variable age and y-variable height. These are expected to be present in every subsequent 'geom' call (think of a geom as a type of plot, although it actually refers to its geometry) * + denotes addition of 'layers' to the plot; we add the individual trajectories with geom_line(), where group = Subject indicates that separate lines be drawn for each subject * the average line (with default confidence envelope) is drawn using a smoother (geom_smooth); group = 1 indicates that we want the smooth over all subjects. Additional properties (also plot elements) such as color and thickness (size) are added to make the average line stand out from the individual trajectories. * Note that variables are mapped to aesthetics, but plot elements can also be set to fixed values by defining them outside aes(). This is why, for example, ggplot2 'knew' to produce separate lines for each subject, because the group aesthetic was mapped to values of Subject. For more about ggplot2, see its on-line help page at http://had.co.nz/ggplot2 Scroll to the bottom of the page to find them. HTH, Dennis On Sun, Feb 20, 2011 at 12:59 PM, dadrivr <dadrivr@gmail.com> wrote:> > Hi all, > > I am trying to plot the fitted trajectories for each individual from an > individual growth model (fit with a linear mixed effects model in lme). > How > can I plot each person's trajectory in the *same* panel, along with the > mean-level trajectory? > > Below is an image of a plot similar to what I'm trying to create (from: > http://jpepsy.oxfordjournals.org/content/31/10/1002/F6.large.jpg): > http://r.789695.n4.nabble.com/file/n3315494/fitted_trajectories.jpg > > Thanks so much for your help! > -- > View this message in context: > http://r.789695.n4.nabble.com/Plotting-individual-trajectories-from-individual-growth-model-tp3315494p3315494.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]]