-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathanimation.R
47 lines (33 loc) · 1.39 KB
/
animation.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Let's create fake data.
library(SVGAnnotation)
years = 1900:2008
n = length(years)
groupIds = LETTERS[1:7]
numGroups = length(groupIds)
alpha = seq(0, by = 10, length = numGroups)
beta = sample(seq(-1, 1, length = numGroups))
sigma.x = seq(.5, 5, length = numGroups)
sigma.y = sample(sigma.x)
data = do.call("rbind",
lapply(seq(along = alpha),
function(i) {
x = alpha[i] + beta[i]*(1:n) + rnorm(n, sigma.x[i])
y = alpha[i] + beta[i]*x + rnorm(n, sigma.y[i])
data.frame(x = x, y = y)
}))
data$year = rep(years, numGroups)
data$groupIds = factor(rep(groupIds, each = n))
radii = sapply(rnorm(numGroups, diff(range(data$x))/100), max, .1)
########################################
filename = "anim.xml"
i = seq(1, nrow(data), by = n)
doc = svgPlot({
with(data, plot(y ~ x, xlim = range(x), ylim = range(y), type = "n", xlab = "X", ylab = "Y"))
abline(v = pretty(data$x), col = "lightgrey", lty = 3)
abline(h = pretty(data$y), col = "lightgrey", lty = 3)
symbols(data$x[i], data$y[i], circles = radii, inches = FALSE,
bg = rainbow(length(levels(data$groupIds[i])))[data$groupIds[i]], add = TRUE)
})
animate(doc, data, data$groupIds, dur = n/.5,
colors = character(), points = getPlotPoints(doc)[1:numGroups], doc = doc)
saveXML(doc, filename)