Animation / Interactive plots in R for inclusion in html websites or latex documents (beamer)

LaTeX
R
Statistics
Teaching
Author

Vinh Nguyen

Published

May 10, 2009

<../../files/Consistency-Ani.gif>

Been wanting to do an interactive/animated plot like the one here for a while (go to the Taylor example). Sage looks like a good program to generate animated graphics, but R is my primary language of choice as a statistician. I don't want to learn another language just for a single feature (although the other features look good as well). For mathy stuff that require the likes of Mathematica (cannot be done in R), I'll go to Sage. But for now, I'll stick with R.

Check out the animation package in R and check out the examples here. It's quite easy to create animations in R. Just create a series of plot (in png or jpeg format) and make them interactive via the convert command from ImageMagick to pack them into a gif file. We can also use mencoder to turn a series of plot into an avi file or convert to turn them into an mpeg file for inclusion in LaTeX documents or slides.

Some code:

```{r}
library(animation)
setwd("~/Documents/UCI/RWorkAndExamples/")
storagefolder <- 'Animations-CI'
if( !(storagefolder %in% dir()) ) dir.create(storagefolder)
outfolder <- file.path(getwd(), storagefolder)
oopt <- ani.options(interval=.05, nmax=100, outdir=outfolder, ani.type="jpeg", ani.dev=jpeg, filename='CI-Animations.html') ##store old options first
ani.start()
conf.int(0.95)
ani.stop()
```
##addprefix script:
#! /bin/bash
addtofile='prefix';
for file in $@
do
mv $file $addtofile.$file;
done
echo "Now run rename to change prefix."

##run in shell
##vinh's script
addprefix *.jpeg
rename prefix. 00 prefix.?.jpeg
rename prefix. 0 prefix.??.jpeg
rename prefix. 00 prefix.?.jpeg
rename prefix. "" prefix.???.jpeg

Check out following this and this to convert image files into movies (for inclusion into pdf via multimedia or movie package):

mencoder "mf://*.jpeg" -mf fps=5 -o CI-animations.avi -ovc lavc -lavcopts vcodec=msmpeg4v2:vbitrate=800

Check out this to convert image files to gif animation for use on websites:

convert -delay 50 -loop 50 ??0.jpeg CI-animations.gif
```{r}
##check following
saveMovie(expr={for(i in 1:10) plot(rnorm(100))}, outdir=outfolder)

ani.options(nmax=100)
saveMovie(expr=clt.ani(obs=300), outdir=outfolder)
flip.coin()

###Taylor example
ifelse1 <- function(test, yes, no) if(test) yes else no

sin.taylor <- function(x) x - x\^3/factorial(3) + x\^5/factorial(5) - x\^7/factorial(7)
sin.taylor2 <- function(x, n=NULL){
 if(is.null(n)) sin(x) else{
 x +ifelse1(n>=3, - x\^3/factorial(3), 0) + ifelse1(n>=5, x\^5/factorial(5), 0) + ifelse1(n>=7, - x\^7/factorial(7), 0) + ifelse1(n>=9, x\^9/factorial(9), 0) + ifelse1(n>=11, - x\^11/factorial(11), 0)
 }
 }
curve(sin, xlim=c(-2*pi,2*pi), ylim=c(-2,2))
x <- seq(-2*pi, 2*pi, .1)
lines(x, f.taylor(x), col="blue", lwd=2)
lines(x, f.taylor2(x, n=7), col="blue", lwd=2)

jpeg("sinTaylor%03d.jpeg")
for(i in c(1,3,5,7,9,11)){
curve(sin, xlim=c(-2*pi,2*pi), ylim=c(-2,2), main=paste("Taylor Expansion of f(x)=sin(x)\nOrder = ",i))
lines(x, sin.taylor2(x, n=i), col="blue", lwd=2)
legend(4,2, legend=c("f", expression(hat(f))), lwd=c(1,2), col=c("black", "blue"), bty="n")
 }
dev.off()
##shell:
convert -delay 100 -loop 50 sinTaylor00?.jpeg sinTaylor.gif
mencoder "mf://sinTaylor00?.jpeg" -mf fps=1 -o sinTaylor.avi -ovc lavc -lavcopts vcodec=msmpeg4v2:vbitrate=800


##consistency
n <- 200
x <- runif(n, 0, 4)
y <- .2*exp(x) + rnorm(n,2)
jpeg("Regression%03d.jpeg")
plot(x,y, col="red")
fit <- lm(y~x)
abline(fit, lwd=2, col="blue")
dev.off()

set.seed(123)
jpeg("Consistency%03d.jpeg", quality=50)
n.seq <- c(10,20,50,200,1000)
nsim <- 4
for(i in 1:length(n.seq)){
 n <- n.seq[i] 
 for(j in 1:4){
 x <- runif(n, 0, 4)
 y <- .2*exp(x) + rnorm(n,2)
 plot(x,y, col="red", xlim=c(0,4), ylim=c(-1,15), main=paste("n =", n), cex.main=2)
 fit <- lm(y~x)
 abline(fit, lwd=4, col="blue")
 legend(0,15, legend=paste("Slope =", round(fit$coef[2],2)), bty="n",cex=2)
 }
 }
dev.off()
##shell
convert -delay 100 -loop 50 Consistency0*.jpeg Consistency-Ani.gif
mencoder "mf://Consistency0*.jpeg" -mf fps=1 -o Consistency-Ani.avi -ovc lavc -lavcopts vcodec=msmpeg4v2:vbitrate=800
```