Animations

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

Installation

install.packages("animation")
library(animation)

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))

return(sineplot)

}

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

n <- 25

saveGIF({

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

for (i in 1:n){

myplot <- sine_func(i)

print(myplot)

ani.pause()}

}, movie.name = "./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.

saveVideo({

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

for (i in 1:n){

myplot <- sine_func(i)

print(myplot)

ani.pause()}

}, video.name = "./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

Edit | Attach | Watch | Print version | History: r1 | Backlinks | Raw View | More topic actions

 Barrick Lab  >  ComputationList  >  RAnimation

Contributors to this topic Edit topic StellaWang
Topic revision: r1 - 2017-02-23 - 21:53:58 - Main.StellaWang
 
This site is powered by the TWiki collaboration platform Powered by Perl This site is powered by the TWiki collaboration platformCopyright ©2024 Barrick Lab contributing authors. Ideas, requests, problems? Send feedback