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.
library(animation)
### 1. How to setup a simple animation ###
## set some options first
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
## 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, .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 = 'brownian_motion.tex'),
interval = 0.1, nmax = 20)
Donate
As a freelancer (currently working as a contractor) and a dad of three kids, I truly appreciate your donation to support my writing and open-source software development! Your contribution helps me cope with financial uncertainty better, so I can spend more time on producing high-quality content and software. You can make a donation through methods below.
-
Venmo:
@yihui_xie, or Zelle:xie@yihui.name -
Paypal
-
If you have a Paypal account, you can follow the link https://paypal.me/YihuiXie or find me on Paypal via my email
xie@yihui.name. Please choose the payment type as “Family and Friends” (instead of “Goods and Services”) to avoid extra fees. -
If you don’t have Paypal, you may donate through this link via your debit or credit card. Paypal will charge a fee on my side.
-
-
Other ways:
WeChat Pay (微信支付:谢益辉) Alipay (支付宝:谢益辉) 

When sending money, please be sure to add a note “gift” or “donation” if possible, so it won’t be treated as my taxable income but a genuine gift. Needless to say, donation is completely voluntary and I appreciate any amount you can give.
Please feel free to email me if you prefer a different way to give. Thank you very much!
I’ll give back a significant portion of the donations to the open-source community and charities. For the record, I received about $30,000 in total (before tax) in 2024-25, and gave back about $15,000 (after tax).