# global.par doesn't work
set.par <- function(...) {
  par(mar = c(3.1, 3.1, 1.1, 1.1), xaxt = 'n', yaxt = 'n')
  return(invisible(NULL))
}
knitr::knit_hooks$set(set.par = set.par)
# nb setting fig.show = 'animate' here doesn't seem to work
knitr::opts_chunk$set(interval = 0.25, out.height = '25%', fig.width = 4, fig.height = 2.5)
knitr::opts_knit$set(animation.fun = knitr::hook_gifski, global.par = TRUE, base.url = "")

Simple example

library(anim.plots)
anim.plot(1:5, 1:5, col = "green")

times controls the frames

x <- rep(1:100/10, 20) 
times <- rep(1:20, each = 100) # twenty frames with 100 points each
y <- sin(x*times/4)
anim.plot(x, y, times = times, type = "l", col = "orange", lwd = 2)

Incremental animations

symbs <- anim.plot(rep(1:10, 2), rep(2:1, each = 10), window = 1:t, pch = 1:20, ylim = c(0,3), cex = 2,
      col = 1:5, xlab = paste("Plot symbols", 1:20))

Parameters

Example - zooming in

x <- rnorm(4000); y<- rnorm(4000)
x <- rep(x, 40); y <- rep(y, 40)
xlims <- 4*2^(-(1:40/10))
ylims <- xlims <- rbind(xlims, -xlims) 
anim.plot(x, y, times=40, xlim=xlims, ylim=ylims, col=rgb(0,1,.5,.3), pch=19, 
      xaxt = 'n', yaxt = 'n')

Moving window

## discoveries 1860-1959
xlim <- rbind(1860:1959,1870:1969)
anim.plot(1860:1959, discoveries, times = 1:100, xlim = xlim, col = "red", 
      xaxp = rbind(xlim, 10), window = t:(t+10), type = "h", lwd = 8, 
      xaxt = "s", yaxt = "s")

Formula interface

data(ChickWeight)
ChickWeight$chn <- as.numeric(as.factor(ChickWeight$Chick))
tmp <- anim.plot(weight ~ chn + Time, data = ChickWeight, col = as.numeric(Diet), 
      pch = as.numeric(Diet))

Run extra commands

replay(tmp, after = legend("topleft", legend = paste("Diet", 1:4), pch = 1:4, 
      col = 1:4, horiz = TRUE))

Plots available

Histogram

anim.hist(rep(rnorm(5000), 7), times = rep(1:7, each = 5000), 
     breaks = c(5, 10, 20, 50, 100, 200, 500, 1000), main = "Normal distribution")

Animated curve

anim.curve(x^t, times = 10:50 / 10, n = 20)

Contour plot

data(volcano)
tmp <- volcano
tmp[] <- 200 - ((row(tmp) - 43)^2 + (col(tmp) - 30)^2) / 20
cplot <- array(NA, dim = c(87, 61, 20))
cplot[ , , 1] <- tmp
cplot[ , , 20] <- volcano
cplot <- apply(cplot, 1:2, function(x) seq(x[1], x[20], length.out = 20))
cplot <- aperm(cplot, c(2, 3, 1))
anim.contour(z = cplot, times = 1:20, levels = 80 + 1:12 * 10, lty = c(1, 2, 2))

3D animation

cplot2 <- apply(cplot, 1:2, function(x) seq(0, x[20], length.out = 20))
cplot2 <- aperm(cplot2, c(2, 3, 1))
par(mar = rep(0, 4))
anim.persp(z = cplot2, times = 1:20, xlab = "", ylab = "", zlab = "Height", phi = 45,
      theta = 30, speed = 5, border = NA, r = 3, col = "yellowgreen", 
      shade = .5, box = FALSE)

Thanks!



hughjonesd/anim.plots documentation built on May 5, 2021, 3:07 p.m.