animatoR dynamic graphics in R A. Blejec [email protected] National Institute of Biology Ljubljana, Slovenia useR! 2011 A. Blejec [email protected] (NIB) animatoR useR! 2011 1 / 35
Mar 29, 2016
animatoR
dynamic graphics in R
National Institute of BiologyLjubljana, Slovenia
useR! 2011
A. Blejec [email protected] (NIB) animatoR useR! 2011 1 / 35
Outline
1 Introduction
2 Homotopy
3 animatoR functions
4 Examples
5 Advanced examplesFireworksScatterplotQuantilesMatricesDistributionsJoint and marginal distribution
A. Blejec [email protected] (NIB) animatoR useR! 2011 2 / 35
Animation solutions for R :
the main idea is: plot frame by frame
package animation provides an organized environment for subsequentplotting, see AniWiki
A. Blejec [email protected] (NIB) animatoR useR! 2011 3 / 35
Drawbacks
plotting everything can be slow for complex figures
smooth movements require intermediate plots, thus lot of frames toplot
A. Blejec [email protected] (NIB) animatoR useR! 2011 4 / 35
XOR: 1+1=0
A. Blejec [email protected] (NIB) animatoR useR! 2011 5 / 35
XOR: 1+1=0
A. Blejec [email protected] (NIB) animatoR useR! 2011 5 / 35
XOR: 1+1=0
A. Blejec [email protected] (NIB) animatoR useR! 2011 5 / 35
XOR: 1+1=0
A. Blejec [email protected] (NIB) animatoR useR! 2011 5 / 35
XOR: 1+1=0
A. Blejec [email protected] (NIB) animatoR useR! 2011 5 / 35
XOR: 1+1=0
A. Blejec [email protected] (NIB) animatoR useR! 2011 5 / 35
Moving a point, line traces the movement.
A. Blejec [email protected] (NIB) animatoR useR! 2011 6 / 35
Moving a point ...t = 0
●
t = 0.33
●
t = 0.67
●
t = 1
●
A. Blejec [email protected] (NIB) animatoR useR! 2011 7 / 35
Linear homotopy
Start coordinate x0End coordinate x1
For parameter t ∈ [0, 1]
xt = h(x0, x1, t) = (1− t) · x0 + t · x1
A. Blejec [email protected] (NIB) animatoR useR! 2011 8 / 35
Linear homotopywhen: start, end, enter, exit ... life
0 , 1
t
h
0.0 0.2 0.4 0.6 0.8 1.0
x0
x1
●●●●●●●●
●●●●●●●●
●●●●●●●●
●●●●●●●●
●●●●●●●●
●●●●●●●●
●●●●●●●●
●●●●●●●●
●●●●●●●●
●●●●●●●●
●●●●●●●●
●●●●●●●●
●●●●●
0.3 , 0.7
t
h
0.0 0.2 0.4 0.6 0.8 1.0
x0
x1
●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●
0.3 , 0.7 , 0.1 , 0.9
t
h
0.0 0.2 0.4 0.6 0.8 1.0
x0
x1
●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●
0.4 , 0.6 , 0.1 , 0.9
t
h
0.0 0.2 0.4 0.6 0.8 1.0
x0
x1
●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●
A. Blejec [email protected] (NIB) animatoR useR! 2011 9 / 35
Moving pointstpoints()
> for (t in seq(0, 1, length = 100)) {+ newplot(stamp = TRUE)+ tpoints(2, 2, 9, 5, t, pch = 16, cex = 2,+ trace = TRUE)+ }
A. Blejec [email protected] (NIB) animatoR useR! 2011 10 / 35
Timed functions
tpoints (x0, y0, x1 = x0, y1 = y0, t, when, trace = FALSE, trace.col = "grey", ...)
tlines (x0, y0, x1, y1, t, when, ...)
tsegments (x0, y0, x1, y1, t, when, fixed = 1, ...)
tarrows (x0, y0, x1, y1, t, when, fixed = 1, length = 0.125, ...)
tpolygon (x0, y0, x1, y1, t, when, ...)
trect (xleft0, ybottom0, xright0, ytop0, xleft1, ybottom1, xright1, ytop1, t, when, ...)
ttext (x0, y0, x1 = x0, y1 = y0, t, when, text = "", ...)
A. Blejec [email protected] (NIB) animatoR useR! 2011 11 / 35
Timed parameters ...
tcex (cex0 = 1, cex1 = 1, t, when, ...)
trgb (x0, x1 = x0, t, when = c(0, 1), alpha0 = 1, alpha1 = 1, ...)
tmatrix (X1, X0 = diag(nrow(X1)), t, when, ...)
A. Blejec [email protected] (NIB) animatoR useR! 2011 12 / 35
Function animator()
animator (block, life = 1, fps = 25, pause = 0.5, verbose = FALSE)
Accepts argument block as a character string
animator('newplot()tpoints(2,2,9,5,trace=TRUE,pch=16,cex=2)
')
A. Blejec [email protected] (NIB) animatoR useR! 2011 13 / 35
Function animator()
Argument block as an expression
> block <- expression({+ newplot()+ tpoints(2, 2, 9, 5, trace = TRUE, pch = 16,+ cex = 2)+ })> animator(block)
A. Blejec [email protected] (NIB) animatoR useR! 2011 14 / 35
Class animator
A block of commands (expression or character version) can be changed toa class animator
> x <- as.animator(block, life = 2)> x
expression({newplot()tpoints(2, 2, 9, 5, trace = TRUE, pch = 16, cex = 2)
})attr(,"class")[1] "animator"attr(,"life")[1] 2
> is.animator(x)
[1] TRUE
> plot(x)
A. Blejec [email protected] (NIB) animatoR useR! 2011 15 / 35
Animated graphics in a PDF fileuse package animate in LaTeX
> plot(as.animator(block))> includeLatex(vspace = "-2cm")
A. Blejec [email protected] (NIB) animatoR useR! 2011 16 / 35
Moving points tpoints()
A. Blejec [email protected] (NIB) animatoR useR! 2011 17 / 35
Lines tlines()
A. Blejec [email protected] (NIB) animatoR useR! 2011 18 / 35
Growing segments tsegments()
A. Blejec [email protected] (NIB) animatoR useR! 2011 19 / 35
Growing arrows tarrows()
A. Blejec [email protected] (NIB) animatoR useR! 2011 20 / 35
Polygons tpolygon()
A. Blejec [email protected] (NIB) animatoR useR! 2011 21 / 35
Rectangles trect()
A. Blejec [email protected] (NIB) animatoR useR! 2011 22 / 35
Fireworks
A. Blejec [email protected] (NIB) animatoR useR! 2011 23 / 35
Projection to the first principal component
A. Blejec [email protected] (NIB) animatoR useR! 2011 24 / 35
Move points to median (0.0, 0.3)then expand to mid-quintiles (0.6, 1.0)
A. Blejec [email protected] (NIB) animatoR useR! 2011 25 / 35
Finding quartiles
A. Blejec [email protected] (NIB) animatoR useR! 2011 26 / 35
Matrix multiplication: points move
A. Blejec [email protected] (NIB) animatoR useR! 2011 27 / 35
Matrix multiplication: change of direction and magnitude
A. Blejec [email protected] (NIB) animatoR useR! 2011 28 / 35
Matrix multiplication: find the eigenvectors
A. Blejec [email protected] (NIB) animatoR useR! 2011 29 / 35
Overlapped distributions
A. Blejec [email protected] (NIB) animatoR useR! 2011 30 / 35
Overlapped distributions
A. Blejec [email protected] (NIB) animatoR useR! 2011 31 / 35
From bivariate distribution to marginal distributions
A. Blejec [email protected] (NIB) animatoR useR! 2011 32 / 35
From marginal distributions to bivariate ?
A. Blejec [email protected] (NIB) animatoR useR! 2011 33 / 35
From marginal distributions to bivariate ?
A. Blejec [email protected] (NIB) animatoR useR! 2011 34 / 35
From marginal distributions to bivariate ?
A. Blejec [email protected] (NIB) animatoR useR! 2011 35 / 35