"R" you ready?, and kindly contributed to Want to share your content on R-bloggers? [This article was first published on, and kindly contributed to R-bloggers ]. (You can report issue about the content on this page here Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Yesterday I surfed the web looking for 3D wireframe examples to explain linear models in class. I stumbled across this site where animated 3D wireframe plots are outputted by SAS. Below I did something similar in R. This post shows the few steps of how to create an animated .gif file using R and ImageMagick. Here I assume that you have ImageMagick installed on your computer. As far as I know it is also possible to produce animated .gif files using R only, e.g. with write.gif() from the caTools package. But using ImageMagick is straighforward, gives you control over the conversion and .gif production and is the free standard program for conversion.

First a simple countdown example. To be sure not to overwrite anything I will create a new folder and set the working directory to the new folder﻿.

dir.create("examples") setwd("examples") # example 1: simple animated countdown from 10 to "GO!". png(file="example%02d.png", width=200, height=200) for (i in c(10:1, "G0!")){ plot.new() text(.5, .5, i, cex = 6) } dev.off() # convert the .png files to one .gif file using ImageMagick. # The system() function executes the command as if it was done # in the terminal. the -delay flag sets the time between showing # the frames, i.e. the speed of the animation. system("convert -delay 80 *.png example_1.gif") # to not leave the directory with the single jpeg files # I remove them. file.remove(list.files(pattern=".png"))

Above a loop is used to do the plotting. A new .png file for each plot is created automatically. The "%02d" part in the filenamepart is a placeholder here for a two character counter (01,02 etc.). So we do not have to hard-code the filename each time.