Creating a Movie from a Series of Plots in R

GraphicsVideoRMovieclipMovies

Graphics Problem Overview


Is there an easy way to create a "movie" by stitching together several plots, within R?

Graphics Solutions


Solution 1 - Graphics

Here is one method I found using R help:

To create the individual image frames:

jpeg("/tmp/foo%02d.jpg")
for (i in 1:5) {
  my.plot(i)
}
dev.off()

To make the movie, first install ImageMagick. Then call the following function (which calls "convert", part of ImageMagick I suppose):

make.mov <- function(){
     unlink("plot.mpg")
     system("convert -delay 0.5 plot*.jpg plot.mpg")
}

Or try using the ffmpeg function as described in this article (I've found this gives cleaner results): ffmpeg -r 25 -qscale 2 -i tmp/foo%02d.jpg output.mp4

May require a bit of tinkering, but this seemed pretty simple once everything was installed.

Of course, anywhere you see "jpg" or "jpeg", you can substitute GIF or PNG to suit your fancy.

Solution 2 - Graphics

Take a look at either the animation package created by Yihui Xie or the EBImage bioconductor package (?animate).

Solution 3 - Graphics

I think you can do this also with the write.gif function in the caTools library. You'd have to get your graph into a multi-frame image first. I'm not sure how to do that. Anyone? Bueller?

The classic example of an animated GIF is this code which I didn't write but I did [blog about][1] some time ago:

library(fields) # for tim.colors
library(caTools) # for write.gif
m = 400 # grid size
C = complex( real=rep(seq(-1.8,0.6, length.out=m), each=m ), imag=rep(seq(-1.2,1.2, length.out=m), m ) )
C = matrix(C,m,m)

Z = 0
X = array(0, c(m,m,20))
for (k in 1:20) {
Z = Z^2+C
X[,,k] = exp(-abs(Z))
}

image(X[,,k], col=tim.colors(256)) # show final image in R
write.gif(X, 'Mandelbrot.gif', col=tim.colors(256), delay=100)

Code credit goes to Jarek Tuszynski, PhD. [1]: http://www.cerebralmastication.com/?p=33

Solution 4 - Graphics

If you wrap your R script within a larger Perl/Python/etc. script, you can stitch graphs together with your favorite command-line image stitching tool.

To run your R script with a wrapper script, use the R CMD BATCH method.

Solution 5 - Graphics

I'm not sure it is possible in R. I did a project once when data points from R were exported to a MySQL database and a Flex/Flash application picked up those data points and gave animated visualizations..

Solution 6 - Graphics

I've done some movies using XNview's (freeware graphics viewer) Create Slideshow function. I wanted to show trends through time with spatial data, so I just created a series of plots, named sequentially [paste() is your friend for all sorts of naming calistethics] then loaded them into XNviews slideshow dialogue and set a few timer variables, voila. Took like 5 minutes to learn how to do it and produce some executable graphics.

Solution 7 - Graphics

Here's a full example on making an animated GIF "movie" from an HDF5 file. The data should be an HDF Dataset of a 3 dimensional array [Nframes][Nrows][Ncolumns].

#
# be sure to be run as Administrator to install new packages
#
source("http://bioconductor.org/biocLite.R")
biocLite("rhdf5")
install.packages('caTools')
install.packages('fields')

library(caTools)
library(fields)
library(rhdf5)

x = h5read(file="mydata.h5",name="/Images")
write.gif(x,"movie1.gif",col=rainbow,delay=10,flip=TRUE)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRyan R. RosarioView Question on Stackoverflow
Solution 1 - GraphicsRyan R. RosarioView Answer on Stackoverflow
Solution 2 - GraphicsPaoloView Answer on Stackoverflow
Solution 3 - GraphicsJD LongView Answer on Stackoverflow
Solution 4 - GraphicsAlex ReynoldsView Answer on Stackoverflow
Solution 5 - GraphicsSriView Answer on Stackoverflow
Solution 6 - Graphicskpierce8View Answer on Stackoverflow
Solution 7 - GraphicsMark LakataView Answer on Stackoverflow