Animations Understood

When I first saw a graphic made from Yihui’s animation package (Xie, 2013) I was amazed at the magic and thought “I could never do that”.

Passage of time…

One night I found myself bored and as usual avoiding work. I decided to try learning how to make an animation and an epiphany hit me in the head.

Figure 1: Animated gif of guy getting hit in the head.

Basically I realized the animation package works just like those flipbooks you made as a kid. You know the ones teachers would yell at you for and would lament the waste of tablet paper. Go on and enjoy this flipbook and allow yourself to be taken back to 3rd grade. Ahh.

Video: A clever use of a tablet.

Now where were we. Ah yes, animation, an electronic flipbook for dorky people. Basically here’s how it works:

  1. Create a scene (static components in a plot)
  2. Create element(s) that will change
  3. Wrap it all into a function
  4. Use the animation package to output an MP4 video, HTML file or an animated GIF.

Let’s do this…

Set It Up

First source this circle drawing function I stole from John Fox and load the animation package.


Create that function to draw a scene

 FUN <- function(y = 0.8) {
    opar <- par()$mar
    on.exit(par(mar = opar))
    par(mar = rep(0, 4))
    circle(0.5, 0.6, 1, "cm", , 4)
    segments(0.5, 0.2, 0.5, 0.54, lwd = 4)
    segments(0.4, 0, 0.5, 0.2, lwd = 4)
    segments(0.6, 0, 0.5, 0.2, lwd = 4)
    segments(0.5, 0.4, 0.3, 0.5, lwd = 4)
    segments(0.5, 0.4, 0.7, 0.5, lwd = 4)
    points(0.5, y, pch = -9742L, cex = 4, col = "firebrick3")

plot of chunk unnamed-chunk-2
Figure 2: Static plot of guy with a static phone.

This part runs FUN and allows the phone to “drop”.

This is where you supply multiple values to the portions of the graphic that will change. Build a function that runs recursively, outputting multiple graphics.

 oopt <- animation::ani.options(interval = 0.1)

FUN2 <- function() {
    lapply(seq(1.01, 0.69, by = -0.02), function(i) {

## FUN2()

Now save it any of the following formats

saveGIF(FUN2(), interval = 0.1, outdir = "images/animate")

saveVideo(FUN2(), interval = 0.1, outdir = "images/animate", 
    ffmpeg = "C:/Program Files (x86)/ffmpeg-latest-win32-static/ffmpeg-20130306-git-28adecf-win32-static/bin/ffmpeg.exe")

saveLatex(FUN2(), autoplay = TRUE, loop = FALSE, latex.filename = "tester.tex",
    caption = "animated dialogue", outdir = "images/animate", ani.type = "pdf", = "pdf", ani.width = 5, ani.height = 5.5, interval = 0.1)

saveHTML(FUN2(), autoplay = FALSE, loop = FALSE, verbose = FALSE, outdir = "images/animate/new",
    single.opts = "'controls': ['first', 'previous', 'play', 'next', 'last', 'loop', 'speed'], 'delayMin': 0")

Oh yeah here’s the HTML version

Created using the reports (Rinker, 2013) package
Get the .Rmd file here
Just the R code


Rinker TW (2013). reports: Package to asssist in report writing. University at Buffalo/SUNY, Buffalo, New York. version 0.1.3,

Xie Y (2013). animation: A gallery of animations in statistics and utilities to create animations. R package version 2.2,

About tylerrinker

Data Scientist, open-source developer , #rstats enthusiast, #dataviz geek, and #nlp buff
This entry was posted in animation, reports, Uncategorized, visualization and tagged , , , , , , . Bookmark the permalink.

7 Responses to Animations Understood

  1. tylerrinker says:

    note that the answer to this question was instrumental to understanding animation.

  2. Pingback: Somewhere else, part 53 | Freakonometrics

  3. Pingback: Sentence Drawing 2: Part II | TRinker's R Blog

  4. pp says:


    Thanks for making the “magic” obvious 🙂

    Cheers !

  5. joe says:

    I’d like to embedded an animation within a pdf document, using knitr and a Rnw file.
    I’ve seen several examples but most don’t work well.
    They use the latex package ‘animate’. Do I need to use the latex animate alongside the R animation package, or only one of them?


  6. joe says:


    If I want to include an animation on a pdf file (using knitr) should I use the R animation package or the latex animate package? Or both?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s