Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 711
Ryan R. Rosario Avatar asked Aug 19 '09 06:08

Ryan R. Rosario


People also ask

How do I save an animated plot in R?

If you need to save the animation for later use you can use the anim_save() function. It works much like ggsave() from ggplot2 and automatically grabs the last rendered animation if you do not specify one directly.


1 Answers

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.

like image 96
Ryan R. Rosario Avatar answered Oct 11 '22 15:10

Ryan R. Rosario