使用gg_animate创建一个gif

B. *_*ton 4 r ggplot2 ggmap

有没有办法在R中实现gg_animate来生成一个gif.我已经创建了我的ggmaps并保存为PDF.我想创建一个动画,滚动浏览这些地图,可能每个图像保持1或2秒.有没有办法在R中这样做或者我应该使用某种gif创建者 - 如果有任何建议?

非常感谢

Jon*_*oll 17

更新于Jul2018:gganimate()已经过重写,现在已经有了很大的改进.以前的API仅可通过归档版本使用,不应与新版本一起使用.

随着新版本:

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')
Run Code Online (Sandbox Code Playgroud)

生成更平滑的图形

在此输入图像描述

原答案:

我发现gganimate包在这方面做得很好.

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")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

2016年12月更新:gganimate()现在替换gg_animate()并添加cowplot为依赖项(解决问题#32后应自动安装).