【发布时间】:2018-07-16 21:04:28
【问题描述】:
有没有办法在 R 中实现 gg_animate 来生成 gif。我已经创建了我的 ggmap 并保存为 PDF。我想创建一个动画,滚动浏览这些地图,可能在每个图像上保持 1 或 2 秒。有没有办法在 R 中做到这一点,或者我应该使用某种 gif 创建器 - 如果有的话有什么建议吗?
非常感谢
【问题讨论】:
有没有办法在 R 中实现 gg_animate 来生成 gif。我已经创建了我的 ggmap 并保存为 PDF。我想创建一个动画,滚动浏览这些地图,可能在每个图像上保持 1 或 2 秒。有没有办法在 R 中做到这一点,或者我应该使用某种 gif 创建器 - 如果有的话有什么建议吗?
非常感谢
【问题讨论】:
2018 年 7 月更新:gganimate() 经历了rewrite and is now much improved。以前的 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')
生成更平滑的图形
原答案:
我发现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")
2016 年 12 月更新:gganimate() 现在替换 gg_animate() 并添加 cowplot 作为依赖项(应在解决 Issue #32 后自动安装)。
【讨论】:
animation 包的源代码(gganimate 包装)似乎 IM 和 GM 都是可能的选项,它会在尝试命令 convert 时使用任何运行的选项.如果您有两个这样的程序,那么在搜索 PATH 时首先出现的程序将获得优先权。我想你可以明确设置 GM 路径的优先级。
gganimate 文档。 github 上的 README (github.com/dgrtwo/gganimate) 甚至有一个间隔选项的例子来加速动画。