I like to use animated plots in my talks on functional time series, partly because it is the only way to really see what is going on with changes in the shapes of curves over time, and also because audiences love them! Here is how it is done.

For LaTeX, you need to create every frame as a separate graphics file. Here is an example. First the R code:

library(demography) nyears <- length(fr.mort$year) for(i in 1:nyears) {     pdf(paste("figs/frmale",i,".pdf",sep=""),height=4,width=6.5)     x <- fr.mort     if(i>1)         x$rate$male[,1:(i-1)] <- NA     lines(x,series='male',lwd=2,col=1)     dev.off() }

This creates a series of pdf files in the figs directory, named frmale1.pdf, …, frmale191.pdf. In the LaTeX file, you need to load the animate package. Then the following command will do the magic:

\centerline{\animategraphics[controls,buttonsize=0.3cm,width=12.5cm]{6}{"frmale"}{1}{191}}

This is how the graph on slide 2 of this presentation was produced.

For web usage, it is better to produce an animated gif version in R:

library(animation) library(demography) nyears <- length(fr.mort$year) makeplot <- function(){ for(i in 1:nyears) {     x <- fr.mort     if(i>1)         x$rate$male[,1:(i-1)] <- NA     lines(x,series='male',lwd=2,col=1) } } oopt = ani.options(interval = 0, nmax = nyears) saveMovie(makeplot(),interval = 0.1, width = 580, height = 400) ani.options(oopt)

This produces the graphic below.

For an explanation of the colours, see my rainbow plot paper.

The animation package for R also allows graphics to be saved in other formats. SeeAniWiki for some examples of animations.

Question

Thanks for very impressive presentation. I am new be, so i would like to ask you how to prepare the data matrix? as i understand you ploted two column, “male” and “rate”, where the variable “year” appear?

  • #2 written by Rob J Hyndman
    about 2 days ago

    For these graphs I used the facilities in the demography package. But you can do it generally using the matplot() and matlines() commands. Then matplot(age,rates,type=”l”) where rates is a matrix with nrows=length(age).