Best practices for high-definition animated videos in R

The goal is to create a video that will play in full screen (on a screen of 1280 x 800) from two ggplot 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") 

However, the video quality is not satisfactory for full screen mode. I tried to increase the "-b 1000k" , but it seems to me that it only increases the file size and output definition.

  • Which device should I use?
  • How to increase the height of the canvas ( ani.height=800 , it seems to give no result)?

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

enter image description here :

+7
r animation video knitr
source share
2 answers

You ask specifically about the animation package, but if you are interested in an alternative method with which I got good results, you can take a look at ffmpeg.

This is one clip that I wrote using igraph and ffmpeg and posted on youtube - which offers resolutions up to 2160p:

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

This will be a clip consisting of PNG showing two ggplot () s next to each other:

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

The ffmpeg command that I used to glue captured PNGs:

 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 

Detailed information on how to use ffmpeg for this purpose can be found here: http://www.joyofdata.de/blog/hd-clips-with-ffmpeg-for-youtube-and-vimeo/

+5
source share

Instead of passing ani.dev="png" you can go through ani.dev = function(...){png(res=75*grain,...)} , where grain is some number> 1. If you multiply your ani.height (and ani.width if set) by the same grain coefficient, then you actually increase the resolution of the pixel at the output by this factor.

NB: the default resolution of 75 above may be machine dependent, I have not seen documented.

0
source share

All Articles