I'll try to write an article about LaTeX animations (with and without knitr) on my blog in the near future. By the way, is it possible to embed such a pdf animation in a html page, with available controls and without requiring to open the pdf with Acrobat ?
My code is below. I would be glad if anybody had any comment about possible enhancements (about quality rendering, or good practices in general).
\documentclass{beamer}
\mode<presentation> {
\usetheme{Boadilla}
}
\usepackage[buttonsize=1em]{animate}
\usepackage{helvet} % fonts package
\usepackage{graphicx} % Allows including images
\begin{document}
<<setup, echo=FALSE>>=
library(scales)
# polyCurve() allows to shade an area below a curve:
polyCurve <- function(x, y, from, to, n = 50, miny,
col = "red", border = col) {
drawPoly <- function(fun, from, to, n = 50, miny, col, border) {
Sq <- seq(from = from, to = to, length = n)
polygon(x = c(Sq[1], Sq, Sq[n]),
y = c(miny, fun(Sq), miny),
col = col, border = border)
}
lf <- length(from)
stopifnot(identical(lf, length(to)))
if(length(col) != lf)
col <- rep(col, length.out = lf)
if(length(border) != lf)
border <- rep(border, length.out = lf)
if(missing(miny))
miny <- min(y)
interp <- approxfun(x = x, y = y)
mapply(drawPoly, from = from, to = to, col = col, border = border,
MoreArgs = list(fun = interp, n = n, miny = miny))
invisible()
}
@
\begin{frame}
\frametitle{The Gaussian sampler}
% initialisiation :
<<gaussian_anim_params, echo=FALSE>>=
nsims <- 100
eps <- .1
xx <- seq(-4,4,length=150)
yy <- dnorm(xx)
rgba <- col2rgb("gray")
tgray <- rgb(rgba[1,1], rgba[2,1], rgba[3,1], alpha=95, maxColorValue=255)
@
\begin{figure}
<<gaussian_anim, echo=FALSE, fig.width=10, fig.height=6, out.width='.9\\linewidth', fig.show='animate', tidy=FALSE, crop=TRUE, dev='tikz', aniopts='controls'>>=
layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE))
for (i in 1:nsims) {
set.seed(666)
simulation <- rnorm(i)
#
cexpoints <- rep(1,i)
cexpoints[i] <- 1.8
cols <- alpha(rep("red", i), 0.5)
cols[i] <- alpha("red",1)
#
par(mar=c(2,5,2,2))
ats <- seq(-4,4,by=2)
labs <- c("$\\mu-4\\sigma$", "$\\mu-2\\sigma$","$\\mu$","$\\mu+2\\sigma$", "$\\mu+4\\sigma$")
plot(1:nsims, rep(0,nsims), type="n", ylim=c(-4,4), axes=FALSE,
xlab="observation", ylab=NA)
rect(1-eps, -2, nsims+eps, 2,
col=tgray,
border="transparent")
axis(1)
axis(2, las=2, at=ats, labels=labs)
grid(nx=NA,ny=NULL)
points(1:i, simulation, col=cols, cex=cexpoints, pch=19)
abline(h=0, col="blue", lty="dotted")
#
plot(xx, yy, type = "l", lwd=4, col="blue", xlim=c(-4,4),
panel.first =
polyCurve(xx, yy, from = -2, to = 2,
col = tgray, border = tgray),
ylab=NA, xlab=NA,
axes=FALSE,
main="Simulations")
axis(1, at=seq(-4,4,by=2), labels=c("$\\mu-4\\sigma$","$\\mu-2\\sigma$","$\\mu$","$\\mu+2\\sigma$", "$\\mu+4\\sigma$"))
points(x=simulation, y=rep(0,i), col=cols, pch=19, cex=cexpoints)
#
n <- i
hstep <- 1
hist(simulation, breaks=seq(-4,4,by=hstep), ylim=c(0,n*1.02), freq=TRUE, axes=FALSE,
ylab="Proportion", xlab=NA,
main="Histogram")
axis(1, at=seq(-4,4,by=2), labels=c("$\\mu-4\\sigma$","$\\mu-2\\sigma$","$\\mu$","$\\mu+2\\sigma$", "$\\mu+4\\sigma$"))
numlabs <- seq(0,1,by=0.25)
labs <- paste0("$", 100*numlabs, "\\%$")
ats <- n*numlabs
axis(2, las=2, at=ats, labels=labs, cex.axis=0.85, hadj=0.9)
curve(n*dnorm(x), add=TRUE, col="blue", lwd=2, lty="dashed")
#
}
@
\end{figure}
\end{frame}
\end{document}