Creating a Movie from a Series of Plots in R [closed]
Asked Answered
C

7

55

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

Cyano answered 19/8, 2009 at 6:27 Comment(0)
C
42

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.

Cyano answered 19/8, 2009 at 15:7 Comment(6)
You can even keep jpeg() and dev.off() outside the loop -- if you use an appropriate filename as e.g. jpeg("/tmp/foo%02d.png"), R will simply create new files during your loop. No need for you to compute the filename. Makes it even easier.Homes
You should make Dirk's fix and then accept your own answer. Good solution.Bollard
useful ... but finding it hard to understand where the .mpg file is saved after running the function 'make.mov' in R? I'm working inside R studio on a mac platform.Hazing
when i run system("convert -delay 80 *.png example_1.gif") i get an error Invalid Parameter - 80. I specify ani.options( convert = shQuote('C:/Program Files/ImageMagick-7.0.5-Q16/convert.exe') # convert = shQuote('C:/Windows/System32/convert.exe') ). Any ideas ?Confessor
link of article does not exist anymoreTaritariff
@Confessor use shell(cmd="convert -delay 80 *.png example_1.gif")Candycandyce
E
15

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

Eicher answered 19/8, 2009 at 9:11 Comment(1)
Interesting note, I was just looking at the animation package's documentation and noticed that it requires ImageMagick to be installed.Commutation
B
12

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 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.

Bollard answered 19/8, 2009 at 15:44 Comment(2)
I get an error on the last line: "Error: unexpected input in "write.gif(X, “" "Intonate
For the above error - change the quote marks around Mandelbrot.gif in the last line of the code to regular quote marks (ie delete them and type new quote marks in your script)Net
A
1

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.

Adz answered 19/8, 2009 at 6:47 Comment(4)
Why do you need another language to use a command-line tool?Disremember
Well where does require a Perl/Python script? Also, look at Rscript (and littler) as better alternatives to 'R CMD BATCH'.Homes
You don't need another language. You can use a shell like bash. Whatever you want. There are lots of options. I use R CMD BATCH because it is more or less universal across platforms.Adz
I found that it is pretty easy once ImageMagick and ffmpeg are installed.Cyano
T
0

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..

Tungstate answered 19/8, 2009 at 6:40 Comment(5)
You don't need a database. In a loop, save all your images. Then use a command-line tool to stitch them together; imagemagick is one possibility.Homes
Yes, this was the easiest way. I guess due to OS modularity, it really isn't possible to do this within R unless R is compiled with a special library or such.Cyano
This is a clever technique, Srirangan. I learned many years ago that when someone says 'it's not possible', they mean 'I don't know how to do it'. The clever part of the technique is that in a forum such as SO, someone is bound to tell you how to do it. I'm not being sarcastic, by the way. I REALLY think it's a good technique, and I'm going to try it out. Thanks Srirangan.Heartburn
Sure. But it is still essentially the same thing that I said. R can't do it and you are depending on an external application to do so. I citied the case where I had used Flex/ActionScript, Ryan recommended the the use of ImageMagick but in the end you are dependent on an external app. That was my point. I was nowhere claiming that my way was the only way to do it. ;)Tungstate
Invoking both a database and Flash is a double overkill!Deach
L
0

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.

Leelah answered 21/8, 2009 at 3:56 Comment(0)
W
0

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)
Westnorthwest answered 8/7, 2014 at 1:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.