---+ Animations Saves a series of generated images to GIF, Flash, video, HTML, or PDF (LaTeX) formats. ---++ Installation <verbatim>install.packages("animation") library(animation)</verbatim> ---+++ 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). <verbatim>ffmpeg = "C:/Program Files (x86)/Path/to/ffmpeg/file.exe"</verbatim> ---+++ 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 <verbatim>brew install imagemagick</verbatim> ---++ 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. <blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"> # Makes an image for every value of i sine_func <- function(i){ </blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"> sineplot <- ggplot(data.frame(x=c(0,i)), aes(x)) + </blockquote></blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"> stat_function(fun=sin) + </blockquote></blockquote></blockquote></blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"> theme(axis.title = element_text(size=16), </blockquote></blockquote></blockquote></blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"> axis.text = element_text(size=12)) </blockquote></blockquote></blockquote></blockquote></blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"> return(sineplot) </blockquote></blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"> } # Loops through all n images and turns them into a GIF n <- 25 saveGIF({ </blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"> ani.options(interval = 0.15, nmax = n) </blockquote></blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"> for (i in 1:n){ </blockquote></blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"> myplot <- sine_func(i) </blockquote></blockquote></blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"> print(myplot) </blockquote></blockquote></blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"> ani.pause()} </blockquote></blockquote></blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"> }, movie.name = "./whatsyoursine.gif", ani.width = 500, ani.height = 400) </blockquote></blockquote> This produces the following animation: <img width="500" alt="R animation package example" src="http://barricklab.org/twiki/pub/Lab/RAnimation/whatsyoursine.gif" title="Animated Sine Function" height="400" /> 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. <blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"> saveVideo({ </blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"> ani.options(interval = 0.15, nmax = n) </blockquote></blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"> for (i in 1:n){ </blockquote></blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"> myplot <- sine_func(i) </blockquote></blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"> print(myplot) </blockquote></blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"> ani.pause()} </blockquote></blockquote></blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"> }, video.name = "./whatsyoursine.mp4", other.opts = "-pix_fmt yuv420p -b:v 500K", #ffmpeg = "C:/Program Files (x86)/Path/to/ffmpeg/executable/file.exe", </blockquote><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px"> ani.width = 500, ani.height = 400) </blockquote> 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 <a target="_blank" href="https://cran.r-project.org/web/packages/animation/animation.pdf">R animation package original documentation</a> --Main.StellaWang - 23 Feb 2017
E
dit
|
A
ttach
|
Watch
|
P
rint version
|
H
istory
: r1
|
B
acklinks
|
V
iew topic
|
M
ore topic actions
Barrick Lab
>
ComputationList
>
RAnimation
Contributors to this topic
StellaWang
Topic revision: r1 - 2017-02-23 - 21:53:58 - Main.StellaWang
Barrick Lab
Contact
Research
Publications
Team
Protocols
Reference
Software
UT Austin
Mol Biosciences
ILS
Microbiology
EEB
CSSB
CBRS
The LTEE
iGEM team
SynBioCyc
SynBio course
NGS course
BEACON
Search
Log in
Copyright ©2025 Barrick Lab contributing authors. Ideas, requests, problems?
Send feedback