Data Science

Spaghetti Plots

Spaghetti plots are a useful data visualisation technique particularly in longitudinal analysis. This post provides some sample spaghetti plots together with the annotated R code used to create them. For an excellent treatment on longitudinal analysis and the role of spaghetti plots read:

Bolger, N., & Laurenceau, J.-P. (2013). Intensive Longitudinal Methods: An Introduction to Diary and Experience Sampling Research (Methodology in the Social Sciences) (p. 256). The Guilford Press. Retrieved from

Example Plots

Below are some examples of spaghetti plots showing results for 3 different participants in the same longitudinal study. The thin grey lines are linear trends for all participants in the study, the orange line the overall study group trend, the thick grey line a cubic group trend and the thick blue line is the participant’s linear trend. The blue circles represents actual participant observations.




Sample Code

The R code to create the spaghetti plots is below and includes annotations. As always, there may be neater ways to do some of these things, but at least here is one way that works. You can modify this code to incorporate your own data sources.

Leave a Reply