7

Is there a way to implement gg_animate in R to produce a gif. I already have my ggmaps created and saved as PDFs. I would like to create an animation which scrolls through those maps maybe holding on each image for 1 or 2 seconds. Is there a way to do this in R or should I use some sort of gif creator - if so any suggestions?

Thanks much

B. Washington
  • 139
  • 1
  • 6

1 Answers1

21

Update Jul2018: gganimate() has undergone a rewrite and is now much improved. The previous API is only available through the archived version and should not be expected to work with the new version.

With the new version:

library(gapminder)
library(gganimate)

## standard ggplot2
ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, colour = country)) +
  geom_point(alpha = 0.7, show.legend = FALSE) +
  scale_colour_manual(values = country_colors) +
  scale_size(range = c(2, 12)) +
  scale_x_log10() +
  # Here comes the gganimate specific bits
  labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy') +
  transition_time(year) +
  ease_aes('linear')

Producing the much smoother graphic

enter image description here

Original Answer:

I found the gganimate package to do quite well at this.

library(gapminder)
library(ggplot2)
theme_set(theme_bw())
p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = continent, frame = year)) +
  geom_point() +
  scale_x_log10()

library(gganimate)

gganimate(p)

gganimate(p, "output.gif")

enter image description here

Update Dec2016: gganimate() now replaces gg_animate() and adds cowplot as a dependency (should auto-install once Issue #32 is resolved).

Jonathan Carroll
  • 3,897
  • 14
  • 34
  • I keep running into an issue that occurs on Windows and has to do with the fact that there are two convert.exe files. Can I use GraphicsMagick instead of ImageMagick with gganimate? – B. Washington Feb 15 '16 at 00:53
  • Digging through the source of the `animation` package (which `gganimate` wraps) it seems that IM and GM are both possible options, and it will use whichever runs when it tries the command `convert`. If you have two such programs, then whichever appears first in a search of `PATH` will get priority. I suppose you could explicitly set the GM path with precedence. – Jonathan Carroll Feb 15 '16 at 01:00
  • 1
    gg_animate is no longer the right name. I also get error no package cowplot – userJT Dec 15 '16 at 16:09
  • 1
    @userJT updated to reflect breaking changes and note missing dependency. – Jonathan Carroll Dec 20 '16 at 23:14
  • is there a way to set the speed of the gif ? – rafa.pereira Mar 21 '17 at 21:22
  • 1
    @rafa.pereira please refer to the `gganimate` documentation. The README on github (https://github.com/dgrtwo/gganimate) even has an example of the interval option to speed up the animation. – Jonathan Carroll Mar 21 '17 at 21:37