--- title: Adding GIF animations author: Alison Presmanes Hill date: '2018-01-31' slug: gif-animations categories: [] tags: [] raw: "https://raw.githubusercontent.com/rbind/blogdown-demo/master/content/post/2018-01-31-adding-gif-animations.Rmd" --- This post will show you how to include GIFs in a **blogdown** post from a website, and how to make new GIFs from R plots (base R and **ggplot2**) using **knitr** chunk options. # Embed GIFs from a link You can add a link to any GIF online, from sites like [giphy](https://giphy.com/), using markdown syntax in either .md or .Rmd posts. For example, to embed this GIF: ![](https://media.giphy.com/media/xUOxf7XfmpxuSode1O/giphy.gif) You would find the GIF [online](https://giphy.com/gifs/broadcity-comedy-central-season-4-xUOxf7XfmpxuSode1O), then click on "Copy Link", and select the "GIF Link" to copy: ![](/images/giphy-gif-link.png) Then use markdown to refer to that link in the plain text of your post (not an R code chunk): ``` ![](https://media.giphy.com/media/xUOxf7XfmpxuSode1O/giphy.gif) ``` # Embed plot GIFs using R To create a GIF animation of a plot made in R, you need to install [FFmpeg](https://www.ffmpeg.org/download.html) on your computer. If you are on a macOS, you can install FFmpeg through [Homebrew](https://brew.sh) using the formula [`brew install ffmpeg`](http://brewformulas.org/Ffmpeg). You can make a GIF animation using **knitr** starting in [version 1.15](https://github.com/yihui/knitr/blob/master/NEWS.md). A GIF animation of the plots in a code chunk will be generated for HTML output when you use the chunk options `fig.show = 'animate'`, `ffmpeg.format = 'gif'`, and `dev = 'jpeg'`. Here is an example **knitr** chunk with options: ````markdown `r ''````{r chunk-label, fig.show='animate', ffmpeg.format='gif', dev='jpeg'} for (i in 1:10) plot(runif(100), ylim = c(0, 1)) # for example ``` ```` Which would produce the GIF below... ```{r random, fig.show='animate', ffmpeg.format='gif', dev='jpeg', echo = FALSE} set.seed(0) for (i in 1:10) plot(runif(100), ylim = c(0, 1)) ``` Let's break those chunk options down: * [`fig.show = 'animate'`](https://yihui.name/knitr/options/#plots): how to show/arrange the plots; `animate` wraps all plots into an animation if there are mutiple plots in a chunk * [`ffmpeg.format = 'gif'`](https://yihui.name/knitr/options/#animation): the filename of the extension video you want to create. This `.gif` file is also saved in your `/static/post/` [directory](https://github.com/rbind/blogdown-demo/tree/master/static/post/2018-01-31-adding-gif-animations_files/figure-html). * [`dev = 'jpeg'`](https://yihui.name/knitr/options/#plots): the function name which will be used as a graphical device to record the individual plots that make up the above. These `.jpeg` files are each saved in your `/static/post/` [directory](https://github.com/rbind/blogdown-demo/tree/master/static/post/2018-01-31-adding-gif-animations_files/figure-html). You can also create GIFs using the [**purrr**](http://purrr.tidyverse.org) and [**ggplot2**](http://ggplot2.tidyverse.org/index.html) packages. ```{r message = FALSE, warning = FALSE} library(purrr) library(ggplot2) library(repurrrsive) # for the example data below ``` Below is some sample code from an awesome [**purrr** tutorial](https://github.com/cwickham/purrr-tutorial) by [Charlotte Wickham](http://cwick.co.nz). From her `map2()` [example code](https://github.com/cwickham/purrr-tutorial/blob/master/04-map2.R): ```{r} gap_split_small <- gap_split[1:10] countries <- names(gap_split_small) # For all countries plots <- map2(gap_split_small, countries, ~ ggplot(.x, aes(year, lifeExp)) + geom_line() + labs(title = .y) + coord_cartesian(ylim = c(0, 100))) ``` To show these plots as an animation, we would use this code chunk: ````markdown `r ''````{r gapgif, fig.show='animate', ffmpeg.format='gif', dev='jpeg'} # Display all plots walk(plots, print) ``` ```` Which produces the GIF below... ```{r gapgif, fig.show='animate', ffmpeg.format='gif', dev='jpeg', echo = FALSE} # Display all plots walk(plots, print) ```