Animations Understood

[This article was first published on TRinker's R Blog » R, 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.

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.


To leave a comment for the author, please follow the link and comment on their blog: TRinker's R Blog » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)