The goal of ggplot.spaghetti is to aid preliminary data investigation into longitudinal/time-series data through visualization. By being able to create plots using different grouping variable, the investigator can have a better idea which variables may be worthwhile to control or include in a hypothesis test. Also these images can be used to help rely to other non-statistical collaborators the trends from a mixed-effects or other type of longitudinal model.
You can install ggplot.spaghetti from github via the devtools
package with:
install.packages("devtools")
devtools::install_github("williazo/ggplot.spaghetti")
time
in the function must be numericalpha
=[0, 1]This is an example using the Orthodont
data set from the nlme
package. Children were measured at 8, 10, 12, and 14 years to determine the distance from the pituitary to the pterygomaxillary fissure in millimeters. We can group these time measurements by Gender
, and I have also created another binary variable, Race
, to highlight the ability to look at multiple grouping variables at the same time.
Reading in the dataset and creating a random variable to represent a binary racial cateogry as White vs. Non-White.
library(ggplot.spaghetti)
library(nlme)
data("Orthodont")
Orthodont = data.frame(Orthodont, Race = rep(ifelse(rbinom(n = 27, size = 1, prob = 0.5)==0, "White", "Non-White"), each = 4))
attach(Orthodont)
In order to look at trends for a categorical variable with overall trends plotted within a single image specify only the group
option as shown below.
ortho_plot_group <- ggplot_spaghetti(y = distance, id = Subject, time = age,
alpha = 0.3, group = Sex, method = "lm")+
xlab("Age (yrs.)")+
ylab("Distance")+
scale_color_grey(name = "Gender", start = 0.0, end = 0.5)+
scale_linetype_manual(name = "Gender", values = c("dashed", "solid"))
ortho_plot_group
In order to look at trends for categorical variables with overall trends plotted in separate plots specify only the wrap
option as shown below.
#specifying just wrap
ortho_plot_wrap <- ggplot_spaghetti(y = distance, id = Subject, time = age,
alpha = 0.3, wrap = Race, method = "loess")+
xlab("Age (yrs.)")+
ylab("Distance")+
scale_color_grey(name = "Race", start = 0.0, end = 0.5)+
scale_linetype_manual(name = "Race", values = c("dashed", "solid"))
ortho_plot_wrap
In order to look at trends for a potential interaction between two categorical variables with overall trends specify both the group
and wrap
option as shown below.
#specifying both group and wrap
ortho_plot <- ggplot_spaghetti(y = distance, id = Subject, time = age,
alpha = 0.3, group = Sex, wrap = Race,
method = "glm")+
xlab("Age (yrs.)")+
ylab("Distance")+
scale_color_grey(name = "Gender", start = 0.0, end = 0.5)+
scale_linetype_manual(name = "Race", values = c("dashed", "solid"))
ortho_plot
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.