Skip to content

Instantly share code, notes, and snippets.

@HarlanH
Created March 11, 2010 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HarlanH/329514 to your computer and use it in GitHub Desktop.
Save HarlanH/329514 to your computer and use it in GitHub Desktop.
demonstration of animating R/ggplot2 graphs with ImageMagick
# NOTE: requires ImageMagick to be installed and in the PATH! Should work on most Linux and Mac systems.
library(ggplot2)
d <- data.frame(x=rep(1:10,10), y=rnorm(100, 1:10))
files <- character()
for (i in 1:10)
{
p <- ggplot(subset(d, subset=x<=i), aes(x=x, y=y)) + geom_point() +
scale_x_continuous(limits=c(0,11)) + scale_y_continuous(limits=c(0,11))
ggsave(filename=(files[i]=sprintf("frame%03d.png", i)), plot=p)
}
cmd <- paste("convert -delay 100", paste(files, collapse=" "), "animated.gif")
system(cmd)
file.remove(unlist(files))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment