layout() with tikz device and animate show

176 views
Skip to first unread message

Stéphane Laurent

unread,
Jul 31, 2013, 1:02:42 PM7/31/13
to kn...@googlegroups.com
Hello, 

 I'm making a pdf animation with knit using a code like this:
```
  \begin{figure}[t]
<<gaussian, echo=FALSE, fig.width=10, fig.height=5, out.width='.9\\linewidth', fig.show='animate', tidy=FALSE, crop=TRUE, dev='tikz'>>=
par(mfrow=c(1,2))
  for (i in 1:25) {
...
```

It works well. But using `layout()` instead of `par(mfrow())` that does not work. Isn't it possible to use `layout()` ?

Yihui Xie

unread,
Jul 31, 2013, 4:21:49 PM7/31/13
to Stéphane Laurent, kn...@googlegroups.com
Can you test the development version? https://github.com/yihui/knitr#readme

There was indeed a problem with layout() after R 3.0.0, and I tried to
fix it, but I guess the fix has not been pushed to CRAN yet.

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Phone: 206-667-4385 Web: http://yihui.name
Fred Hutchinson Cancer Research Center, Seattle
> --
> You received this message because you are subscribed to the Google Groups
> "knitr" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to knitr+un...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Stéphane Laurent

unread,
Jul 31, 2013, 4:42:51 PM7/31/13
to kn...@googlegroups.com, Stéphane Laurent

 Hello Yihui. I'm currently using R-2.15.3 (with the latest CRAN version of knitr). Was there this problem with R-2.15.3 too ?

Yihui Xie

unread,
Jul 31, 2013, 4:44:30 PM7/31/13
to Stéphane Laurent, kn...@googlegroups.com
Sorry I do not remember and cannot tell. I first noticed the problem
when R 3.0 came out. The current development version should be
compatible with R 2.15.x.

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Phone: 206-667-4385 Web: http://yihui.name
Fred Hutchinson Cancer Research Center, Seattle


Stéphane Laurent

unread,
Jul 31, 2013, 5:00:31 PM7/31/13
to kn...@googlegroups.com, Stéphane Laurent
I was trying with R-3.0.1 but I don't know how to install the tikzDevice package.

Stéphane Laurent

unread,
Jul 31, 2013, 5:09:01 PM7/31/13
to kn...@googlegroups.com, Stéphane Laurent
Yeeeaahhh Yihui, that works with R-2.15.3 and the development version of knitr. Thanks !!

Yihui Xie

unread,
Jul 31, 2013, 6:08:17 PM7/31/13
to Stéphane Laurent, kn...@googlegroups.com
That was just asked the other day in the r-help list:
http://r.789695.n4.nabble.com/tikzDevice-td4672523.html but I guess it
is slightly more complicated for Windows: you will have to install
RTools.

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Phone: 206-667-4385 Web: http://yihui.name
Fred Hutchinson Cancer Research Center, Seattle


Stéphane Laurent

unread,
Aug 1, 2013, 2:19:06 AM8/1/13
to kn...@googlegroups.com, Stéphane Laurent
After installing the required filehash package I get the error message below. 

> install.packages('tikzDevice', repos='http://r-forge.r-project.org', type='source')
Content type 'application/x-gzip' length 1733547 bytes (1.7 Mb)
opened URL
downloaded 1.7 Mb

* installing *source* package 'tikzDevice' ...
** libs
ERROR: compilation failed for package 'tikzDevice'
* removing 'U:/ProgramFiles/R/R-3.0.1/library/tikzDevice'

The downloaded source packages are in
        ‘C:\Temp\RtmpYPHDgB\downloaded_packages’
Warning messages:
1: running command '"U:/ProgramFiles/R/R-3.0.1/bin/i386/R" CMD INSTALL -l "U:\ProgramFiles\R\R-3.0.1\library" C:\Temp\RtmpYPHDgB/downloaded_packages/tikzDevice_0.6.3.tar.gz' had status 1 
2: In install.packages("tikzDevice", repos = "http://r-forge.r-project.org",  :
  installation of package ‘tikzDevice’ had non-zero exit status
 

Stéphane Laurent

unread,
Aug 1, 2013, 2:32:16 AM8/1/13
to kn...@googlegroups.com, Stéphane Laurent
I've just opened the question on the tikzDevice google group:  https://groups.google.com/forum/#!topic/tikzdevice/GT3OGBxuyKc

Yihui Xie

unread,
Aug 1, 2013, 3:01:15 AM8/1/13
to Stéphane Laurent, kn...@googlegroups.com
I guess that google group is pretty much dead.

I just tested it under Windows, and I did not have any problems with
the installation. As I have told you in the previous reply, you have
to install Rtools, and make sure the C compilers (e.g. gcc) are in
your PATH.

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Phone: 206-667-4385 Web: http://yihui.name
Fred Hutchinson Cancer Research Center, Seattle


Stéphane Laurent

unread,
Aug 1, 2013, 3:21:36 AM8/1/13
to kn...@googlegroups.com, Stéphane Laurent
Thanks, but I don't have admin privileges in the computer and I have never been able to use Rtools. 

Yihui Xie

unread,
Aug 1, 2013, 3:33:30 AM8/1/13
to Stéphane Laurent, kn...@googlegroups.com
Some software packages do not really require system admin privileges;
I installed Rtools on my department Windows server, and I do not have
admin privilege. I guess that depends on how much restriction your
system admin has put on your servers. It is always frustrating to
communicate with admins, though. Personally I also hate Windows
servers; it is just hard to get anything done there.

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Phone: 206-667-4385 Web: http://yihui.name
Fred Hutchinson Cancer Research Center, Seattle


Stéphane Laurent

unread,
Aug 1, 2013, 4:48:28 AM8/1/13
to kn...@googlegroups.com, Stéphane Laurent
I have installed Rtools without problem but I don't know how to use it. 

Yihui Xie

unread,
Aug 1, 2013, 4:59:18 AM8/1/13
to Stéphane Laurent, kn...@googlegroups.com
I do not believe your system admin cannot really stop you. Sometimes
it is fun to fight with Windows; you will learn a lot about otherwise
useless tricks.

The key: learn to use the command setx to set environment variables. Good luck!

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Phone: 206-667-4385 Web: http://yihui.name
Fred Hutchinson Cancer Research Center, Seattle


Stéphane Laurent

unread,
Aug 1, 2013, 5:12:49 AM8/1/13
to kn...@googlegroups.com, Stéphane Laurent
Y'es I'm sure this is possible because when I installed pandoc with a msi installer, the environment variable has been correctly set. And I agree it's fun to fight with Windows ;)
Thanks for your help.

Stéphane

Yihui Xie

unread,
Aug 1, 2013, 3:09:08 PM8/1/13
to Stéphane Laurent, kn...@googlegroups.com
Glad to know that.

Just realized I had a typo in the last reply:

> I do not believe your system admin _can_ really stop you.

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Phone: 206-667-4385 Web: http://yihui.name
Fred Hutchinson Cancer Research Center, Seattle


Stéphane Laurent

unread,
Aug 1, 2013, 3:41:55 PM8/1/13
to kn...@googlegroups.com, Stéphane Laurent
These tools are really great : https://dl.dropboxusercontent.com/s/94k4geni54q9psm/gaussienne_Beamer_Knitr_Animate.pdf

It would have taken me infinitely more time to make the animation with Asymptote. 

 I don't know how to express my gratitude to all my favorite R developpers :-/

Stéphane Laurent

unread,
Aug 1, 2013, 3:43:48 PM8/1/13
to kn...@googlegroups.com, Stéphane Laurent
Sorry I've forgotten to share the code (using the scales package) :

<<echo=FALSE>>=
  nsims <- 150
@
  \begin{figure}[t]
<<gaussian, echo=FALSE, fig.width=10, fig.height=6, out.width='.9\\linewidth', fig.show='animate', tidy=FALSE, crop=TRUE, dev='tikz'>>=
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)
    #
    ats <- seq(-4,4,by=2)
    labs <- c("", "$\\mu-2\\sigma$","$\\mu$","$\\mu+2\\sigma$",  "")
        plot(1:nsims, rep(0,nsims), type="n", ylim=c(-4,4), axes=FALSE, 
         xlab="observation", ylab=NA)
    axis(1)
    axis(2, at=ats, labels=labs)
    grid(nx=NA,ny=NULL)
    points(1:i, simulation, col=cols, cex=cexpoints, pch=19)
    #
    curve(dnorm(x), from=-4, to=4, axes=FALSE, xlab=NA, ylab=NA, lwd=3, col="blue", 
          main="Simulations")
    labs <- c(expression(mu-4~sigma), expression(mu-2~sigma), expression(mu),  expression(mu+2~sigma),  expression(mu+4~sigma))
    axis(1, at=seq(-4,4,by=2), labels=paste0("$", seq(-4,4,by=2), "\\sigma$"))
    points(x=simulation, y=rep(0,i), col=cols, pch=19, cex=cexpoints)
    #
    hist(simulation, breaks=seq(-4,4,by=1), ylim=c(0,1), freq=FALSE, 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$"))
    ats <- seq(0,1,by=0.25)
    axis(2, at=ats, labels=paste0("$", 100*ats, "\\%$"))
    curve(dnorm(x), add=TRUE, col="blue", lwd=2, lty="dashed")
    #
    #
  }
@
  \end{figure}

Stéphane Laurent

unread,
Aug 31, 2013, 3:15:39 AM8/31/13
to kn...@googlegroups.com
Better like this: https://www.dropbox.com/s/prmqkpk92ks8s2e/gaussian_anim_solo.pdf
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}


Yihui Xie

unread,
Aug 31, 2013, 5:00:34 PM8/31/13
to Stéphane Laurent, knitr
That is possible, but not straightfoward. It requires very good
understanding of knitr (I do not want to promote it, but the knitr
book will be helpful). Two key steps are:

1. convert tikz/PDF to PNG; you can use ImageMagick/convert for this
2. embed images in HTML; use knitr::hook_scianimator() as the
animation function instead of the default hook_ffmpeg_html()

You can see plenty of examples at http://vis.supstat.com/ which were
generated from knitr, but I do not have time to describe everything in
this reply; the website is open source on Github, so you can probably
figure it out by yourself.

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA
Reply all
Reply to author
Forward
0 new messages