# Plotting changes in a three-valued ordinal variable across two time points using R

I have a dataset with 4025 participants across two time points. I have scored them on a three-point categorical variable (Unlikely, Possible, Probable) at each time point. I would like to visualize the various patterns of change (e.g. going from Unlikely at T1 to Possible at T2 or going from Possible at T1 to Unlikely at T2). I would also like some way of representing the number of participants in each of these clusters on the graph (somehow weighing by N and representing this by line thickness, number of lines etc.).

The data is currently on the form:

id1, id2, variable_t1, variable_t2
1     500        0            1
2     501        1            0
...


Any suggestions for how to do this? I have tried using ggplot2 and geom_line and grouping by id, but this graph looks very messy. I am looking for something more along the lines of a clustergram, but am open to suggestions.

Update: I recently discovered Parallel Sets which is very close to what I would like to achieve. The only downside to this program is that it allows for very little customization of the resulting plot (e.g. rotating plot, adding titles and axis labels, manually adjusting size etc.). Although this is possible with a bit of post processing of the png file that the program can export to. (Now, is there a way of achieving the same with R and ggplot?).

Solution: thanks to a reposting of my question by Tal (of R-bloggers fame) here there is now a solution for this question using R and lattice.

• With just "two time points" you can describe the entire dataset with a 3 by 3 table (giving the counts of the nine possible transitions), which is hardly a challenge to display or visualize! How does your situation differ from this? – whuber Jun 14 '11 at 13:54
• Thanks for the suggestion, but I was looking for a method that allowed me to track each individual's transition across time, and not just the group as a whole - or am I not understanding you correctly? – Tormod Jun 16 '11 at 6:14
• Great (!) question Tormod. I've re-asked your updated question here: stats.stackexchange.com/questions/12029/… so to be sure it will get attention (I'd love to use such a code for the clustergram) Cheers, Tal – Tal Galili Jun 17 '11 at 11:15
• Thanks for reposting Tal! I see that you have already gotten a solution using R - super! – Tormod Jun 20 '11 at 8:29

One of your options is to use a sunflowerplot for each combination. This is available from a default installation of R. For some datasets, a sunflowerplot is not particularly clear, so I have used colour coding instead.

If R is your thing, the code below should get you going with the colour coding (just copy-and-pasted from an old utility function I had lying around, so it may require some editing):

plotTwoCats2<-function (x, y = NULL, type = "p", xlim = NULL, ylim = NULL,
log = "", main = NULL, sub = NULL, xlab = NULL, ylab = NULL,
ann = par("ann"), axes = FALSE, frame.plot = axes, panel.first = NULL,
panel.last = NULL, asp = NA, ...)
{
ttt<-table(data.frame(x,y))
tt<-data.frame(ttt)
#print(names(tt))
tt$x<-as.factor(tt$x)
tt$y<-as.factor(tt$y)
usr<-par("usr"); on.exit(par(usr))

minFreq<-min(tt$Freq) maxFreq<-max(tt$Freq)
numX<-length(levels(tt$x)) numY<-length(levels(tt$y))
#par(usr=c(0, numX, 0, numY))
ppin <- par("pin")
xsize<-ppin[1] / (length(levels(x)) + 1)
ysize<-ppin[2] / (length(levels(y)) + 1)
plot(as.numeric(x),as.numeric(y),
xlim=c(0.5,length(levels(x)) + 0.5),
ylim=c(0.5,length(levels(y)) + 0.5),
log = log, main = main, sub = sub,
xlab = xlab, ylab = ylab, ann = ann, axes = axes, frame.plot = frame.plot,
panel.first = panel.first, panel.last = panel.last, asp = asp, ...)

sapply(seq(numX), function(xi){
sapply(seq(numY), function(yi){
frq<-tt$Freq[(as.numeric(tt$x)==xi) & (as.numeric(tt\$y)==yi)]
freqpct<-(frq-minFreq)/(maxFreq-minFreq)
clr<-rgb(freqpct, 1-freqpct, 0, 1)
rect(-0.5+xi, -0.5+yi, 0.5+xi, 0.5+yi, col=clr)
text(xi, yi, labels=frq)
invisible()
})
invisible()
})
invisible()
}


With 3 categories you can use a trilinear plot:

Allen, Terry. Using and Interpreting the Trilinear Plot.  Chance.
15 (Summer 2002).


The article shows an example of change in 3 categories over time (as well as many other examples).

The triplot function in the TeachingDemos package for R does these plots as well as triangle.plot in ade4, ternaryplot in vcd, tri in cwhtool, and triax.plot in plotrix (and probably a couple of others as well).