useR-Blejec-animatoR

40
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

description

http://ablejec.nib.si/R/useR-Blejec-animatoR.pdf

Transcript of useR-Blejec-animatoR

Page 1: useR-Blejec-animatoR

animatoR

dynamic graphics in R

A. [email protected]

National Institute of BiologyLjubljana, Slovenia

useR! 2011

A. Blejec [email protected] (NIB) animatoR useR! 2011 1 / 35

Page 2: useR-Blejec-animatoR

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

Page 3: useR-Blejec-animatoR

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

Page 4: useR-Blejec-animatoR

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

Page 5: useR-Blejec-animatoR

XOR: 1+1=0

A. Blejec [email protected] (NIB) animatoR useR! 2011 5 / 35

Page 6: useR-Blejec-animatoR

XOR: 1+1=0

A. Blejec [email protected] (NIB) animatoR useR! 2011 5 / 35

Page 7: useR-Blejec-animatoR

XOR: 1+1=0

A. Blejec [email protected] (NIB) animatoR useR! 2011 5 / 35

Page 8: useR-Blejec-animatoR

XOR: 1+1=0

A. Blejec [email protected] (NIB) animatoR useR! 2011 5 / 35

Page 9: useR-Blejec-animatoR

XOR: 1+1=0

A. Blejec [email protected] (NIB) animatoR useR! 2011 5 / 35

Page 10: useR-Blejec-animatoR

XOR: 1+1=0

A. Blejec [email protected] (NIB) animatoR useR! 2011 5 / 35

Page 11: useR-Blejec-animatoR

Moving a point, line traces the movement.

A. Blejec [email protected] (NIB) animatoR useR! 2011 6 / 35

Page 12: useR-Blejec-animatoR

Moving a point ...t = 0

t = 0.33

t = 0.67

t = 1

A. Blejec [email protected] (NIB) animatoR useR! 2011 7 / 35

Page 13: useR-Blejec-animatoR

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

Page 14: useR-Blejec-animatoR

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

Page 15: useR-Blejec-animatoR

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

Page 16: useR-Blejec-animatoR

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

Page 17: useR-Blejec-animatoR

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

Page 18: useR-Blejec-animatoR

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

Page 19: useR-Blejec-animatoR

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

Page 20: useR-Blejec-animatoR

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

Page 21: useR-Blejec-animatoR

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

Page 22: useR-Blejec-animatoR

Moving points tpoints()

A. Blejec [email protected] (NIB) animatoR useR! 2011 17 / 35

Page 23: useR-Blejec-animatoR

Lines tlines()

A. Blejec [email protected] (NIB) animatoR useR! 2011 18 / 35

Page 24: useR-Blejec-animatoR

Growing segments tsegments()

A. Blejec [email protected] (NIB) animatoR useR! 2011 19 / 35

Page 25: useR-Blejec-animatoR

Growing arrows tarrows()

A. Blejec [email protected] (NIB) animatoR useR! 2011 20 / 35

Page 26: useR-Blejec-animatoR

Polygons tpolygon()

A. Blejec [email protected] (NIB) animatoR useR! 2011 21 / 35

Page 27: useR-Blejec-animatoR

Rectangles trect()

A. Blejec [email protected] (NIB) animatoR useR! 2011 22 / 35

Page 28: useR-Blejec-animatoR

Fireworks

A. Blejec [email protected] (NIB) animatoR useR! 2011 23 / 35

Page 29: useR-Blejec-animatoR

Projection to the first principal component

A. Blejec [email protected] (NIB) animatoR useR! 2011 24 / 35

Page 30: useR-Blejec-animatoR

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

Page 31: useR-Blejec-animatoR

Finding quartiles

A. Blejec [email protected] (NIB) animatoR useR! 2011 26 / 35

Page 32: useR-Blejec-animatoR

Matrix multiplication: points move

A. Blejec [email protected] (NIB) animatoR useR! 2011 27 / 35

Page 33: useR-Blejec-animatoR

Matrix multiplication: change of direction and magnitude

A. Blejec [email protected] (NIB) animatoR useR! 2011 28 / 35

Page 34: useR-Blejec-animatoR

Matrix multiplication: find the eigenvectors

A. Blejec [email protected] (NIB) animatoR useR! 2011 29 / 35

Page 35: useR-Blejec-animatoR

Overlapped distributions

A. Blejec [email protected] (NIB) animatoR useR! 2011 30 / 35

Page 36: useR-Blejec-animatoR

Overlapped distributions

A. Blejec [email protected] (NIB) animatoR useR! 2011 31 / 35

Page 37: useR-Blejec-animatoR

From bivariate distribution to marginal distributions

A. Blejec [email protected] (NIB) animatoR useR! 2011 32 / 35

Page 38: useR-Blejec-animatoR

From marginal distributions to bivariate ?

A. Blejec [email protected] (NIB) animatoR useR! 2011 33 / 35

Page 39: useR-Blejec-animatoR

From marginal distributions to bivariate ?

A. Blejec [email protected] (NIB) animatoR useR! 2011 34 / 35

Page 40: useR-Blejec-animatoR

From marginal distributions to bivariate ?

A. Blejec [email protected] (NIB) animatoR useR! 2011 35 / 35