Video output from R script

As an offshoot from one of my dissertation projects I’m developing a simple model to explore protein evolution.  The model takes an amino acid sequence and mutates it, selecting for mutations that result in improvement for one of several measurable protein parameters (flexibility, hydropathy, aliphatic index, isoelectric point, or aromaticity).  I’m using it to explore how selection for one parameter results in unintended changes to the other parameters.  I implemented the model in Python but I’m using R for data analysis and plotting.  For each run I have several hundred measures of the five different protein parameters.  There are a few ways to visualize this, but since the dataset represents a temporal progression it makes sense to create an animation of the output.  After some exploration this is the method that I came up with.

Assuming that you have some table of data (evol_param_d_matrix_1 in this example) in R for which a row represents a single time point, you can generate a figure (I chose a barplot) for each time point as such:

jpeg(filename = 'animations/Rplot%03d.jpg')
for (i in 1:length(evol_param_d_matrix_1[,1])){
  barplot(evol_param_d_matrix_1[i,],
          ylim = c(-0.5, 1.5),
          axisnames = T
          )
}
dev.off()

Note that it is extremely important to specify ylim (and xlim, if this axis might vary for your data) to prevent the scale of the plot changing from image to image.  The above code saves each plot (one per line in evol_param_d_matrix_1) as a jpeg to the directory “animations”, labeled Rplot000.jpg, Rplot001.jpg, etc.  To convert this sequence of still images to video I used the free tool (for linux/mac) ffmpeg.  For mac install using homebrew.  I called ffmpeg using:

ffmpeg -r 15 -qscale 1 -i 'Rplot%03d.jpg' test.mp4

That command tells ffmpeg to use 15 frames per second, with a quality of “1” (the highest quality), on all jpegs matching the given naming pattern.  I first tried to use Rplot*.jpg, but this ordered the frames alphanumerically, rather than numerically. The output file is of course test.mp4.  Changing to test.avi would instead output in that format.  For reasons unknown to me, neither the command tee nor the redirect could capture ffmpegs screen output to a logfile, which might have been useful for the initial troubleshooting.

The ffmpeg documentation is, unfortunately, rather dense and written for people with a lot more audio/visual background than I have.  It looks like ffmpeg is a commonly used tool however, so there are quite a few easy(ish) to follow tutorials online.  Here’s the final product:

ffmpeg animation of barplots produced in R.
5887 Total Views 1 Views Today
This entry was posted in Research. Bookmark the permalink.

One Response to Video output from R script

  1. Avatar photo Jeff says:

    It turns out that windows executables are available for ffmpeg. I could have saved myself several additional steps tarballing the images and shipping them off to a mac for conversion…

    http://ffmpeg.zeranoe.com/builds/

Leave a Reply

Your email address will not be published. Required fields are marked *

WordPress Anti Spam by WP-SpamShield