Aesthetic effects with phantom data

In the last section, I used interpolation, where new data are created between two points, and then plotted to give us a smooth effect in the video. This principle can also be extended purely for aesthetic reasons, which turns out to be one of the most powerful tools at your disposal in making animated graphs. The examples on this page show a scatterplot with trails lagging behind moving markers, and a ripple effect spreading out from a marker at a specific point in time. These are both widely used in bespoke animations and have been the preserve of design / graphics people, but you can also create them directly within your favourite stats software. And even better, once you know how to make these effects, there is nothing to stop you from inventing your own effect using the same technique.

Trails fading behind moving points

First, I'm going to create the same moving scatterplot that we had before, but add trails behind the markers. To do this, we will plot the interpolated data in each frame, and also the previous positions of each marker. If you want the trail to be one second long, you will need to plot the previous 25 frames' co-ordinates. To do this I create phantom data containing these previous co-ordinates. In Stata or SPSS you have to add additional rows to your data file which contain the phantom data, while in R you can save them into a different object and then add them to your plot using a command like points().

The aesthetic effect of a trail dying away behind the moving object (like a comet's tail) is achieved by making the markers for the phantom data lighter in colour (or darker, if you have a trendy dark background) as they get older. This can be done by giving each phantom datum a value controlling the colour, and changing this as the frames move on. In R this can be done with smooth gradations by changing the numbers that go into the graph parameter col. Here's an example of how you can do that:

oldblue<-rgb2hsv(col2rgb("blue"))
newblue[2,1]<-0.5*oldblue[2,1]
plot(1,1,col=hsv(h=newblue[1,1],s=newblue[2,1],v=newblue[3,1]),
          cex=2,pch=19)

In this code, I take blue as a basic colour I want to work with, and convert it to HSV (Hue-Saturation-Value) which is just a system for classifying colours that one can easily tweak to achieve shading effects. To make a n intense colour lighter, reduce Saturation (the second element of the matrix saved as myhsv). To make a pale colour darker, reduce Value instead. So, when I draw the plot, I get one marker at co-ordinates (1,1) which is half the saturation of R's default blue. I have achieved this by simply multiplying the saturation value which is stored at oldblue[2,1] by 0.5. For clarity, I have done this in a long-winded way, but you could be more succinct, for example

newblue<-oldblue%*%matrix(c(1,0.5,1),nrow=3)

So there is nothing complex in the next step of making a gradation:

myhsv<-rgb2hsv(col2rgb("blue"))
x<-seq(from=0.05,to=1,by=0.05)
y<-x
i<-x
plot(x,y,col=hsv(h=myhsv[1,1],s=i*myhsv[2,1],v=myhsv[3,1]),
          cex=2,pch=19,ylim=c(-10,10),xlim=c(-10,10))

The result of this R code, a little blue comet

Or you could make a small tweak to get a pale trail on a trendy dark background.

myhsv<-rgb2hsv(col2rgb("wheat"))
x<-seq(from=0.05,to=1,length.out=20)
y<-x
i<-x
plot(x,y,ylim=c(-1,2),xlim=c(-1,2))
rect(par("usr")[1],par("usr")[3],par("usr")[2],par("usr")[4],col="black")
points(x,y,col=hsv(h=myhsv[1,1],s=myhsv[2,1],v=i*myhsv[3,1]),
          cex=3,pch=19)

Don't you know dark backgrounds are SO 2011?

To achieve the same effect in Stata, you have to superimpose multiple -twoway scatter- plots, which each one having the mcolor() option multiplied by a different intensity value, which gets tedious after a while. I find that six different levels of gradation are enough to give the impression of smoothness, although you might want more to get a really slick look. You can have more, you just have to write more lines in your do-file and wait a bit longer for it to plot.

Click here for the full R code for trails, and here for the data file.

Ripples spreading out from points

Another very popular effect is to have ripples spreading outward from your markers at a certain point in time to draw attention to them. This is easily achieved by including phantom data at the points where the ripples should be centered, and increasing the radius steadily. Click here for the R code.