animation with gridSVG

November 12, 2011

(This article was first published on Omnia sunt Communia! » R-english, and kindly contributed to R-bloggers)

Some time ago I posted a question in stackoverflow to learn how to produce a multipage SVG file. This is easy if you want a PDF:

trellis.device(pdf, file='iris.pdf')
p <- xyplot(Sepal.Length~Petal.Length|Species, data=iris, layout=c(1, 1))

But the PDF file is not really useful to get something similar to a slideshow. There wasn’t a definitive answer but the best comment was: “to make R generate a bunch of SVGs and then manually make some html page with javaScript doing the page switching”. I finally decided to ask directy to Paul Murrell, the creator of grid and gridSVG (recently available at CRAN). He kindly provided some good advices and code which completely solved the problem with an approach similar to the advice from stackoverflow. I modified his code to write the function animateTrellis:


animateTrellis <- function(object, file='animatedSVG.svg',
                           duration=.1, step=2, show=TRUE){
  nLayers <- dim(object)
  for (i in seq_len(nLayers)){
    p <- object[i]
    label <- p$condlevels[[1]][i]
    ##Create intermediate SVG files
    g <- grid.grabExpr(print(p, prefix=label),
    if (i==1){ ## First frame
      ga <- animateGrob(g, group=TRUE,
                        duration=duration, begin=step)
    } else if (i==nLayers){ ##Last Frame
      gg <- garnishGrob(g, visibility='hidden')
      ga <- animateGrob(gg, group=TRUE,
                        duration=duration, begin=step*(i-1))
    } else { ##any frame
      gg <- garnishGrob(g, visibility='hidden')
      gaV <- animateGrob(gg, group=TRUE,
                         duration=duration, begin=step*(i-1))
      ga <- animateGrob(gaV, group=TRUE,
                        duration=duration, begin=step*i)
    fich <- tempfile(fileext='.svg')

    ## Combine all
    if (i==1) {
      svgTop <- xmlParse(fich)
      nodeTop <- getNodeSet(svgTop,
    } else {
      svgChildren <- xmlParse(fich)
      node <- getNodeSet(svgChildren,
      addChildren(nodeTop, node)
  saveXML(svgTop, file=file)
  if (show) browseURL(file)

Now you can produce a SVG file with animation from the previous trellis object:

animateTrellis(p, file='iris.svg')

You will find lot of information and examples of gridSVG in this article.

To leave a comment for the author, please follow the link and comment on their blog: Omnia sunt Communia! » R-english. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)