Saves a series of generated images to GIF, Flash, video, HTML, or PDF (LaTeX) formats.



On Windows

If you are planning to make an animation in video format on Windows, you will have to specify where ffmpeg is located. This would require you to include the following argument to the example code (will demonstrate later in the Example section).

ffmpeg = "C:/Program Files (x86)/Path/to/ffmpeg/file.exe"

On Mac OS

If you are planning to make an animation in GIF format, you should the following to install ImageMagick, which is what R uses to make GIFs

brew install imagemagick

Quick explanation

The animation package in R works basically the same as a regular flipbook that takes several images and puts them together at an interval to generate an animation. The save functions available are all pretty similar in terms of their arguments: an expression that generates each image, options for the animation such as interval, image size, and total number of frames, name to save animation under, and additional options to pass to the executable that actually produces the file.

Example (GIF format)

Start with your expression that produces the individual images you would like to turn into an animation. A good way to do this is to create a function that makes an individual image given an input and then to loop through all the images in the save animation function.

# Makes an image for every value of i

sine_func <- function(i){

sineplot <- ggplot(data.frame(x=c(0,i)), aes(x)) +

stat_function(fun=sin) +

theme(axis.title = element_text(size=16),

axis.text = element_text(size=12))



# Loops through all n images and turns them into a GIF

n <- 25


ani.options(interval = 0.15, nmax = n)

for (i in 1:n){

myplot <- sine_func(i)



}, = "./whatsyoursine.gif", ani.width = 500, ani.height = 400)

This produces the following animation:

R animation package example

In general, GIF format is quite large. Video formats tend to have more customizability in terms of video quality, so they are more space efficient (you can also scroll through them). To make the same animation but in video format, you would use the following.


ani.options(interval = 0.15, nmax = n)

for (i in 1:n){

myplot <- sine_func(i)



}, = "./whatsyoursine.mp4", other.opts = "-pix_fmt yuv420p -b:v 500K",

#ffmpeg = "C:/Program Files (x86)/Path/to/ffmpeg/executable/file.exe",

ani.width = 500, ani.height = 400)

If you are on Windows and you wanted to produce a video, you would have to uncomment the "ffmpeg =" line above and make the path refer to your ffmpeg installation.

Detailed documentation

R animation package original documentation

--Main.StellaWang - 23 Feb 2017

 Barrick Lab  >  WebLeftBar  >  ComputationList  >  RAnimation

Topic revision: r1 - 23 Feb 2017 - 21:53:58 - Main.StellaWang
This site is powered by the TWiki collaboration platformCopyright ©2017 Barrick Lab contributing authors. Ideas, requests, problems? Send feedback