|
| 1 | +--- |
| 2 | +output: |
| 3 | + md_document: |
| 4 | + variant: markdown_github |
| 5 | +--- |
| 6 | + |
| 7 | +## gganimate: Create easy animations with ggplot2 |
| 8 | + |
| 9 | +<!-- README.md is generated from README.Rmd. Please edit that file --> |
| 10 | + |
| 11 | +```{r, echo = FALSE} |
| 12 | +knitr::opts_chunk$set( |
| 13 | + collapse = TRUE, |
| 14 | + comment = "#>", |
| 15 | + fig.path = "README/README-fig-", |
| 16 | + cache.path = "README/README-cache-" |
| 17 | +) |
| 18 | +``` |
| 19 | + |
| 20 | + |
| 21 | +```{r echo = FALSE} |
| 22 | +library(knitr) |
| 23 | +
|
| 24 | +# I want the README to have visible GIFs on GitHub, as |
| 25 | +# GitHub cannot show .mp4s or other animation formats. |
| 26 | +# I therefore hacked together a GIF animation hook for knitr. |
| 27 | +
|
| 28 | +library(animation) |
| 29 | +ani.options(autobrowse = FALSE) |
| 30 | +
|
| 31 | +opts_knit$set(animation.fun = function(x, options, format = "gif") { |
| 32 | + x = c(knitr:::sans_ext(x), knitr:::file_ext(x)) |
| 33 | + fig.num = options$fig.num |
| 34 | + format = sub("^[.]", "", format) |
| 35 | + fig.fname = paste0(sub(paste0(fig.num, "$"), "*", x[1]), |
| 36 | + ".", x[2]) |
| 37 | + mov.fname = paste0(sub(paste0(fig.num, "$"), "", x[1]), ".", |
| 38 | + format) |
| 39 | +
|
| 40 | + animation::im.convert(fig.fname, output = mov.fname) |
| 41 | + |
| 42 | + sprintf("", options$label, paste0(opts_knit$get("base.url"), mov.fname)) |
| 43 | +}) |
| 44 | +
|
| 45 | +opts_chunk$set(cache = TRUE, message = FALSE, warning = FALSE, fig.show = "animate") |
| 46 | +``` |
| 47 | + |
| 48 | +**gganimate** wraps the [animation package](http://www.inside-r.org/packages/cran/animation/docs/animation) to create animated ggplot2 plots. |
| 49 | + |
| 50 | +The core of the approach is to treat "frame" (as in, the time point within an animation) as another aesthetic, just like **x**, **y**, **size**, **color**, or so on. Thus, a variable in your data can be mapped to frame just as others are mapped to x or y. |
| 51 | + |
| 52 | +For example, suppose we wanted to create an animation similar to the [Gapminder world](http://www.gapminder.org/world) animation. |
| 53 | + |
| 54 | +```{r pkgs, cache = FALSE} |
| 55 | +library(gapminder) |
| 56 | +library(ggplot2) |
| 57 | +theme_set(theme_bw()) |
| 58 | +``` |
| 59 | + |
| 60 | +```{r setup} |
| 61 | +p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = continent, frame = year)) + |
| 62 | + geom_point() + |
| 63 | + scale_x_log10() |
| 64 | +``` |
| 65 | + |
| 66 | +Once we have created that plot (notice it was saved as `p`), we display it as an animation with the `gg_animate` function: |
| 67 | + |
| 68 | +```{r dependson = "setup"} |
| 69 | +library(gganimate) |
| 70 | +
|
| 71 | +gg_animate(p) |
| 72 | +``` |
| 73 | + |
| 74 | +This displays each of the frames of the plot in sequence (note that if you're running it interactively it will be too fast to see all of them). When combined with knitr's `fig.show = "animate"`, we create an animation as seen above. |
| 75 | + |
| 76 | +You can also save the animation to a file, such as an GIF, video, or an animated webpage: |
| 77 | + |
| 78 | +```{r eval = FALSE} |
| 79 | +gg_animate(p, "output.gif") |
| 80 | +gg_animate(p, "output.mp4") |
| 81 | +gg_animate(p, "output.swf") |
| 82 | +gg_animate(p, "output.html") |
| 83 | +``` |
| 84 | + |
| 85 | +(Each of these requires ffmpeg, ImageMagick, or other such drivers to be installed on your computer: see the [animation package](http://www.inside-r.org/packages/cran/animation/docs/animation) documentation for more). |
| 86 | + |
| 87 | +Notice that the axis limits and legend stay fixed between animation frames, even though the points move. This is one of the advantages of the **gganimate** package as opposed to creating animations manually: the plot (and its axes and legend) is built only once but rendered for each frame. |
| 88 | + |
| 89 | +### Customization |
| 90 | + |
| 91 | +You can have some layers of your plot animated and others not, simply by adding the `frame` aesthetic to those layers and not others. This is useful, for example, if you want to *highlight* particular points in an animation rather than move them. |
| 92 | + |
| 93 | +```{r} |
| 94 | +p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop)) + |
| 95 | + geom_point() + |
| 96 | + geom_point(aes(frame = year), color = "red") + |
| 97 | + scale_x_log10() |
| 98 | +
|
| 99 | +gg_animate(p) |
| 100 | +``` |
| 101 | + |
| 102 | +Note that while animating over time is intuitive, you could match any variable in your data to the `frame` aesthetic. We could animate across continents instead: |
| 103 | + |
| 104 | +```{r} |
| 105 | +p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, frame = continent)) + |
| 106 | + geom_point() + |
| 107 | + scale_x_log10() |
| 108 | +
|
| 109 | +gg_animate(p) |
| 110 | +``` |
| 111 | + |
| 112 | +Finally, note that if there is a stat summarization (such as a `geom_smooth`) that you want to animate, you should also add a `group` aesthetic to that layer. Otherwise, the layer will be calculated once across all frames (and therefore be constant in the animation): |
| 113 | + |
| 114 | +```{r} |
| 115 | +p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, frame = year)) + |
| 116 | + geom_point() + |
| 117 | + geom_smooth(aes(group = year), method = "lm", show.legend = FALSE) + |
| 118 | + facet_wrap(~ continent, scales = "free") + |
| 119 | + scale_x_log10() |
| 120 | +
|
| 121 | +gg_animate(p) |
| 122 | +``` |
| 123 | + |
| 124 | +Note also that you can control your animation the same way you would in the animation package, using the [ani.options](http://www.inside-r.org/packages/cran/animation/docs/ani.options) function: |
| 125 | + |
| 126 | +```{r eval = FALSE} |
| 127 | +library(animation) |
| 128 | +ani.options(interval = .3) |
| 129 | +gg_animate(p, "output.gif") |
| 130 | +``` |
| 131 | + |
0 commit comments