Spaghetti Plots for Visualizing a Continuous Dep. Var.

#This is a way to visualize your dataset prior to doing any longitudinal modeling

#This assumes your data is already in “long” format

#First load dataset

mydata<-read.csv(“~/mydata.csv”)

#Then, do Spaghetti plots with “ggplot2”, combine with “gridExtra”:

#First load essential packages:

library(ggplot2)
library(gridExtra) #allows you to combine ggplots

#Create a theme for your plots (optional):

mytheme <- theme_classic() %+replace%
  theme(axis.title.x = element_blank(),
        axis.title.y = element_text(face=”bold”,angle=90))
#figure out how to split up the spag plot
quantile(mydata$subject_id)
#create 4 plots with 25% of the data in each:
p1 <- ggplot(data = subset(mydata, subject_id %in% c(1:25)),
            aes(x = time.var, y = dep.var, group=subject_id)) +
            mytheme +
            ggtitle(“1:25”) +
            geom_line()
p2 <- ggplot(data = subset(mydata, subject_id %in% c(26:50)),
            aes(x = time, y = dep.var, group=subject_id)) +
            mytheme +
            ggtitle(“26:50”) +
            geom_line()
p3 <- ggplot(data = subset(mydata, subject_id %in% c(51:75)),
            aes(x = time, y = dep.var, group=subject_id)) +
            mytheme +
            ggtitle(“51:75”) +
            geom_line()
p4 <- ggplot(data = subset(mydata, subject_id %in% c(76:100)),
            aes(x = time, y = dep.var, group=subject_id)) +
            mytheme +
            ggtitle(“76:100”) +
            geom_line()
#combine plots:
grid.arrange(p1,p2,p3,p4, ncol=2)
#You can also use “plotly”, which creates cool interactive graphs
install.packages(“plotly”)
library(plotly)
plot_ly(mydata,
        type=’scatter’,
        x=~time.var, y=~dep.var,
        text = paste(“ID: “, mydata$subject_id),
        mode = “markers”, color=~categorical_covariate, size=~continuous_covariate)
#If you want to do it 3D, then you can just add a “z”, e.g.:
plot_ly(mydata,
        type=’scatter3d’,
        x=~time.var, y=~dep.var, z=~continuous_covariate
        text = paste(“ID: “, mydata$subject_id),
        mode = “markers”, color=~categorical_covariate, size=~continuous_covariate)
#NOTE: the 3D graph doesn’t work too well in RStudio
#I recommend knitting it to HTML with RMarkdown!
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s