Using gg_animate to create a gif

半城伤御伤魂 提交于 2020-01-01 14:36:35

问题


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


回答1:


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

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")

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



来源:https://stackoverflow.com/questions/35399182/using-gg-animate-to-create-a-gif

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!