| animation-package {animation} | R Documentation |
This package contains a variety functions for animations in statistics which could probably aid in teaching statistics and data analysis; it also has several utilities to export R animations to other formats.
This package mainly makes use of HTML & JavaScript and R
windows graphics devices (such as
x11) to demonstrate animations
in statistics; other kinds of output such as Flash (SWF)
or GIF animations or PDF animations are also available if
required software packages have been installed. See below
for details on each type of animation.
It's natural and easy to create an animation in R using
the windows graphics device, e.g. in x11() or
windows(). A basic scheme is like the Example 1
(see below).
On-screen animations do not depend on any third-party software, but the rendering speed of the windows graphics devices is often slow, so the animation might not be smooth (especially under Linux and Mac OS).
The generation of HTML animation pages does not rely on
any third-party software either, and we only need a web
browser to watch the animation. See
saveHTML.
The HTML interface is just like a movie player – it comes with a series of buttons to control the animation (play, stop, next, previous, ...).
This HTML approach is flexible enough to be used even in
Rweb, which means we do not really have to install R to
create animations! There is a demo in
system.file('misc', 'Rweb', 'demo.html', package =
'animation'). We can use saveHTML to
create animations directly in Rweb; this can be helpful
when we do not have R or cannot install R.
If ImageMagick or GraphicsMagick has been installed, we
can use im.convert or
gm.convert to create a GIF animation
(combining several R plots together), or use
saveGIF to create a GIF animation from an R
code chunk.
If SWF Tools has been installed, we can use
saveSWF to create a Flash animation (again,
combining R plots).
If LaTeX is present in the system, we can use
saveLatex to insert animations into a PDF
document and watch the animation using the Adobe reader.
The animation is created by the LaTeX package
animate.
The function saveVideo can use FFmpeg to
convert images to various video formats (e.g. ‘mp4’,
‘avi’ and ‘wmv’, etc).
Bug reports and feature requests can be sent to https://github.com/yihui/animation/issues.
Yihui Xie <http://yihui.name>
The associated website for this package: http://animation.yihui.name
Yihui Xie and Xiaoyue Cheng. animation: A package for statistical animations. R News, 8(2):23–27, October 2008. URL: http://CRAN.R-project.org/doc/Rnews/Rnews_2008-2.pdf
(NB: some functions mentioned in the above article have been slightly modified; see the help pages for the up-to-date usage.)
Yihui Xie (2013). animation: An R Package for Creating Animations and Demonstrating Statistical Methods. Journal of Statistical Software, 53(1), 1-27. URL http://www.jstatsoft.org/v53/i01/.
saveHTML, saveGIF,
saveSWF, saveVideo,
saveLatex
### 1. How to setup a simple animation ###
## set some options first
oopt = ani.options(interval = 0.2, nmax = 10)
## use a loop to create images one by one
for (i in 1:ani.options("nmax")) {
plot(rnorm(30))
ani.pause() ## pause for a while ('interval')
}
## restore the options
ani.options(oopt)
## see ?ani.record for an alternative way to set up an animation
### 2. Animations in HTML pages ###
saveHTML({
ani.options(interval = 0.05, nmax = 30)
par(mar = c(3, 3, 2, 0.5), mgp = c(2, 0.5, 0), tcl = -0.3, cex.axis = 0.8, cex.lab = 0.8,
cex.main = 1)
brownian.motion(pch = 21, cex = 5, col = "red", bg = "yellow", main = "Demonstration of Brownian Motion")
}, img.name = "bm_plot", title = "Demonstration of Brownian Motion", description = c("Random walk on the 2D plane: for each point",
"(x, y), x = x + rnorm(1) and y = y + rnorm(1)."))
### 3. GIF animations ###
saveGIF({
ani.options(nmax = 30)
brownian.motion(pch = 21, cex = 5, col = "red", bg = "yellow")
}, interval = 0.05, movie.name = "bm_demo.gif", ani.width = 600, ani.height = 600)
### 4. Flash animations ###
saveSWF({
par(mar = c(3, 2.5, 1, 0.2), pch = 20, mgp = c(1.5, 0.5, 0))
buffon.needle(type = "S")
}, ani.dev = "pdf", ani.type = "pdf", swf.name = "buffon.swf", interval = 0.1, nmax = 40,
ani.height = 7, ani.width = 7)
### 5. PDF animations ###
saveLatex({
par(mar = c(3, 3, 1, 0.5), mgp = c(2, 0.5, 0), tcl = -0.3, cex.axis = 0.8, cex.lab = 0.8,
cex.main = 1)
brownian.motion(pch = 21, cex = 5, col = "red", bg = "yellow", main = "Brownian Motion")
}, img.name = "BM_plot", latex.filename = ifelse(interactive(), "brownian_motion.tex",
""), interval = 0.1, nmax = 20)