Skip to content

Instantly share code, notes, and snippets.

@yihui
Created March 23, 2012 03:33
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save yihui/2166508 to your computer and use it in GitHub Desktop.
My Talk on Animations at useR! 2010 (NIST, Gaithersburg)
pdf.options(width = 7, height = 5, family = "AvantGarde")
library(RColorBrewer)
library(animation)
# title page
# pdf('titlepic.pdf', width=7, height=4)
ani.options(nmax = 66, interval = 0)
data(HuSpeech)
par(mar = c(4, 4, 0.1, 0.1))
moving.block(dat = HuSpeech, FUN = function(..., dat = dat,
i = i, block = block) {
plot(..., x = i + 1:block, xlab = "paragraph index", ylim = range(dat),
ylab = sprintf("HuSpeech[%s:%s]", i + 1, i + block))
}, type = "o", pch = 20)
# dev.off()
# Newton's method
# pdf('newton_method_succeed.pdf')
par(pch = 20, mar = c(4, 4, 3, 2))
ani.options(nmax = 50, interval = 1)
newton.method(function(x) 5 * x^3 - 7 * x^2 - 40 *
x + 100, 7.15, c(-6.2, 7.1))
# dev.off()
# pdf('newton_method_fail.pdf')
# par(pch = 20,mar=c(4,4,3,2))
ani.options(interval = 0.5, nmax = 50)
xx = newton.method(function(x) atan(x), rg = c(-5,
5), init = 1.5)
xx$root
# dev.off()
# quincunx demo
# pdf('quincunx.pdf')
set.seed(123)
ani.options(nmax = 200 + 15 - 2, interval = 0.03)
freq = quincunx(balls = 200, col.balls = sample(brewer.pal(9,
"Set1"), 200, TRUE))
# dev.off()
# bootstrap
# pdf('boot_iid.pdf')
par(mar = c(1.5, 3, 1, 0.1), cex.lab = 0.8, cex.axis = 0.8,
mgp = c(2, 0.5, 0), tcl = -0.3)
ani.options(interval = 0, nmax = 40)
boot.iid(main = c("", ""))
# dev.off()
# moving window
# pdf('mwar_ani.pdf')
ani.options(interval = 0, nmax = 50)
par(mar = c(2, 3, 1, 0.5), mgp = c(1.5, 0.5, 0))
mwar.ani(, pch = 21, col = "red", bg = "yellow", type = "o")
# dev.off()
# HTML demo
ani.options(nmax = 100, interval = 0.1, ani.height = 500,
ani.width = 600, loop = FALSE, title = "Simulation of Buffon's Needle",
description = "There are three graphs made in each step: the top-left
one is a simulation of the scenario, the top-right one is to help us
understand the connection between dropping needles and the mathematical
method to estimate pi, and the bottom one is the result for each
dropping.")
ani.start()
par(mar = c(3, 2.5, 1, 0.2), pch = 20, mgp = c(1.5,
0.5, 0))
buffon.needle(type = "S")
ani.stop()
# SVGAnnotation is on Omegahat
# install.packages('SVGAnnotation', repos = 'http://www.omegahat.org/R')
library(SVGAnnotation)
# from example(animate)
p1 = matrix(c(2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5,
6, 6, 6), 5, , byrow = TRUE)
p2 = matrix(c(12, 2, 1, 10, 3, 1.2, 8, 4, 1.4, 6,
5, 1.5, 4, 6, 3), 5, , byrow = TRUE)
p3 = cbind(seq(4, 12, length = 5), rep(6, 5), c(1.5,
3, 5, 3, 1.5))
D = data.frame(rbind(p1, p2, p3))
names(D) = c("x", "y", "radius")
D$time = rep(1:5, 3)
D$id = rep(1:3, each = 5)
D = D[order(D$time), ]
pp = svgPlot({
plot(y ~ x, subset(D, time == 1), pch = 21, col = c("red",
"green", "blue"), bg = c("red", "green", "blue"), xlim = range(D$x),
ylim = range(D$y))
# Add horizontal and vertical lines at suitable positions.
abline(h = c(2, 4, 6), col = "lightgray", lty = 3)
abline(v = c(2, 7, 12), col = "lightgray", lty = 2)
})
animate(pp, D, "time", dropFirst = TRUE, labels = seq(2005,
length = 5), begin = 1, radii = "radius")
saveXML(pp, "gm1.svg")
# use Opera to view gm1.svg
# swfDevice is on R-Forge currently and does not work on Windows
# install.packages('swfDevice', repos = 'http://r-forge.r-project.org')
library(swfDevice)
library(animation)
swf("knn_ani.swf", frameRate = 1, ttf = "vera")
ani.options(interval = 0, nmax = 10)
x = matrix(c(rnorm(80, mean = -1), rnorm(80, mean = 1)),
ncol = 2, byrow = TRUE)
y = matrix(rnorm(20, mean = 0, sd = 1.2), ncol = 2)
knn.ani(train = x, test = y, cl = rep(c("first class",
"second class"), each = 40), k = 30)
dev.off()
library(animation)
ani.options(nmax = 1)
saveMovie({
par(pty = "s", mar = rep(1, 4))
vi.lilac.chaser()
}, interval = 0.05)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment