Animation

If you have a code chunk that generates multiple plots (e.g., using a loop), you can have the output stitched together in an aninmation using the fig.show=‘animate’ code chunk option.

```{r sampleani, fig.show='animate', fig.width=4, fig.height=4, cache=FALSE, interval=0.05, aniopts="controls,loop"}
for (i in 1:50) {
    plot(x=i/10, y=i/10, xlim=c(0,6), ylim=c(0,6), pch=20, col=palette()[2], cex=5)
}
```

Notes:

  • animations are created as video files, so they may only appear in a browser (i.e., not the RStudio viewer)
  • to create the animation, you need to install a command line video encoding utility called ffmpeg. ffmpeg is free and cross-platform. You may download ffmpeg from https://www.ffmpeg.org/download.html. Be sure to place the ffmpeg executable file(s) in a directory on the path.
  • for more options, see Knitr code chunk options

Other Tools and Techniques

In addition to ffmpeg, you can use ImageMagick to stitch together plot() PNG files to a video file. See https://www.r-bloggers.com/animated-plots-with-r/

The animation package has wrapper functions that call on ImageMagick, ffmpeg, SWF Tools, and other 3rd party encoding utilities to create animations.

The gganimate package allows you to create animated versions of plots created with the plotting functions in the popular ggplot2 package. It also uses the animation package, so you’ll need one or more third party encoding utility installed. Code example 1. Code example 2

The tlocoh package has a function to created animations of animal movement data. Samples