Because it’s Friday- animated graphs

I’ve been analysing some data and we’re a bit unsure, as we often are, as to its quality. We have a variety of clinicians rating C.tot and R.tot over a four year period for a set of patients. To summarise, there was some concern that people are rating C.tot consistently and well but not so with R.tot (I’m not going to explain the variables or where they come from because it’s not relevant and it’s an internal dataset we’re not ready to share yet).

I’m sure there is a statistical approach (essentially we need to check to see if the R.tot scores are jumping around) but I thought a graphical approach might suffice for now.

So I talk all the individuals who had been in the dataset for long enough, and put each of their score trajectories on a graph, and then animated so you see the first patient, then the second and the first, then the first second and third, etc.

Actually the R.tot scores look pretty sensible looking at the animation. Have a look and see what you think (click to animate).

So easy to achieve with the mighty ggplot and animation packages from R, here’s all the code


mydf=melt(mytemp, measure.vars=c("R.tot", "C.tot"), id.vars=c("ID", "Week"))

for (i in unique(mydf$ID)) {
  png(filename=paste(i, ".png", sep=""))
    ggplot(subset(mydf, ID <= i), aes(x=Week, y=value)) + 
      geom_line(aes(group=ID)) +
      xlab("Week") + 
      opts(title = "Clinical and risk score over time") +
      facet_wrap(~variable, ncol=1)

## change outdir to the current working directory
ani.options(outdir = getwd(), interval=0.05)

im.convert(paste(unique(mydf$ID), ".png", sep=""), output = "animation.gif")