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.

 source("http://dl.dropboxusercontent.com/u/61803503/wordpress/circle_fun.txt")
library(animation)

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))
    plot.new()
    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")
}

FUN()
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) {
        FUN(i)
        animation::ani.pause()
    })
}

## 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",
    ani.dev = "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


References

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

Xie Y (2013). animation: A gallery of animations in statistics and utilities to create animations. R package version 2.2, http://CRAN.R-project.org/package=animation.

About these ads

About tylerrinker

I am Literacy PhD student with a bent for the quantitative and a passion for R.
This entry was posted in animation, reports, Uncategorized, visualization and tagged , , , , , , . Bookmark the permalink.

4 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:

    Hi,

    Thanks for making the “magic” obvious :)

    Cheers !

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s