# Longitudinal data, 4 years, 3 levels - wanting to visualise transitions

I have a dataframe which in R looks somewhat like this.

set.seed(100)

df <- data.frame(ID = 1:10,
I.2011 = sample(1:3, rep = 1, 10),
I.2012 = sample(1:3, rep = 1, 10),
I.2013 = sample(1:3, rep = 1, 10),
I.2014 = sample(1:3, rep = 1, 10))

ID I.2011 I.2012 I.2013 I.2014
1   1      1      2      2      2
2   2      1      3      3      3
3   3      2      1      2      2
4   4      1      2      3      3
5   5      2      3      2      3
6   6      2      3      1      3
7   7      3      1      3      1
8   8      2      2      3      2
9   9      2      2      2      3
10 10      1      3      1      1


The data is longitudinal, and I am interested in people's transitions over time. Particularly important are the group of people who begin in 2011 in group 2, so perhaps in some visualisation the people who satisfy this criteria would have their trajectory in colour whereas all other trajectories would be in shades of grey.

I am wondering if anyone had a neat solution for visualising these transitions, perhaps implementable in R with ggplot2 or something interactive which would be implementable with d3.js (I am new to d3.js so I'm looking for a chance to try it out!)

If example code is available that would be fantastic, however I'm not so worried about that, and instead wish to focus on how best this information could be visualised. A type of sankey or chord diagram is coming to mind, but overall I am unsure.

## 1 Answer

You might be looking for sequence analysis methods. Have a look at the R package called TraMineR. It might be a good starting place to see what you could do, then try and implement in ggplot or d3.js. At the very least, it will hopefully help you learn some names of plots which you can use and then search for them more effectively. Here is just some of the plotting capabilities in TraMineR:

library(TraMineR)
set.seed(100)
df <- data.frame(ID = 1:100,
I.2011 = sample(1:3, rep = 1, 100),
I.2012 = sample(1:3, rep = 1, 100),
I.2013 = sample(1:3, rep = 1, 100),
I.2014 = sample(1:3, rep = 1, 100))

seq <- seqdef(data = df[,grep("I.",names(df),fixed = T)])

seqiplot(seq, title = "First ten sequences",  tlim=1:10)

seqIplot(seq, sortv = "from.start", title = "Grouping sequences by starting patterns") # this might be of particular interest.

seqfplot(seq, title = "The most common pathways", tlim=1:10)


See http://traminer.unige.c or ?TraMineR in R for more details.