Using gg_animate to create a gif
Asked Answered
M

1

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

Murray answered 14/2, 2016 at 23:11 Comment(0)
O
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).

Oilcan answered 15/2, 2016 at 0:48 Comment(6)
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?Murray
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.Oilcan
gg_animate is no longer the right name. I also get error no package cowplotWend
@Wend updated to reflect breaking changes and note missing dependency.Oilcan
is there a way to set the speed of the gif ?Comate
@Comate please refer to the gganimate documentation. The README on github (github.com/dgrtwo/gganimate) even has an example of the interval option to speed up the animation.Oilcan

© 2022 - 2024 — McMap. All rights reserved.