Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices for high-def animation videos in R

The goal is to produce a video to be played full screen (on a 1280 x 800 display) of two ggplots arranged vertically with grid.arrange(). For example:

library(ggplot2)
library(gridExtra)
library(animation)
saveVideo({
  for (i in 1:50) {
    data = data.frame(x=rnorm(1000),y=rnorm(1000))
    plot1 = ggplot(data, aes(x=x, y=y)) + geom_point()
    plot2 = ggplot(data, aes(x=y, y=x)) + geom_point()

    grid.arrange(arrangeGrob(plot1, plot2, heights=c(3/4, 1/4), ncol=1))

    ani.options(interval = 0.05, ani.dev="png", ani.height=800)
  }
},video.name = "test_png.mp4", other.opts = "-b 1000k")

Yet the quality of the video is not satisfactory for full screen. I tried increasing "-b 1000k" but it seems to me it only increases the size of the file and the definition of the output.

  • Which device should I use?
  • How to increase the hight of the canvas (ani.height=800 doesn't seem to produce any result)?

EDIT: I tried the script with the option other.opts = "-s 1280x800". Although the image now is wider the definition is still low. Here the screenshot (top-to-bottom) taken from my 1280x800 display (compare the video with the menu bar):

enter image description here:

like image 609
CptNemo Avatar asked Aug 24 '14 04:08

CptNemo


1 Answers

You are asking specifically about the animation package but in case you are interested in an alternative method that I yieled good results with you might want to have a look at ffmpeg.

This is one clip I composed using igraph and ffmpeg and posted on youtube - which offers a resolution up to 2160p:

https://www.youtube.com/watch?v=Aga9UxMPuFA

This would be a clip composed of PNGs showing two ggplot()s next to each other:

https://www.youtube.com/watch?v=3A4qZdSf7bk

The ffmpeg command I used for glueing together the captured PNGs is:

ffmpeg 
  -framerate 10     # input frame rate
  -i image%03d.png  # image names (image000.png, ..., image999.png)
  -s:v 1280x720     # video size
  -c:v libx264      # encoder
  -profile:v high   # H.264 profile for video
  -crf 20           # constant rate factor
  -pix_fmt yuv420p  # pixel format
  -r 30             # output frame rate
  clip.mp4   # clip file name

Details on how to use ffmpeg for this purpose you may find here: http://www.joyofdata.de/blog/hd-clips-with-ffmpeg-for-youtube-and-vimeo/

like image 198
Raffael Avatar answered Nov 15 '22 05:11

Raffael