【问题标题】:Using gg_animate to create a gif使用 gg_animate 创建 gif
【发布时间】:2018-07-16 21:04:28
【问题描述】:

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

非常感谢

【问题讨论】:

    标签: r ggplot2 ggmap


    【解决方案1】:

    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 后自动安装)。

    【讨论】:

    • 我一直遇到一个在 Windows 上发生的问题,这与有两个 convert.exe 文件有关。我可以在 gganimate 中使用 GraphicsMagick 代替 ImageMagick 吗?
    • 挖掘animation 包的源代码(gganimate 包装)似乎 IM 和 GM 都是可能的选项,它会在尝试命令 convert 时使用任何运行的选项.如果您有两个这样的程序,那么在搜索 PATH 时首先出现的程序将获得优先权。我想你可以明确设置 GM 路径的优先级。
    • gg_animate 不再是正确的名称。我也收到错误 no package cowplot
    • @userJT 已更新以反映重大更改并注意缺少的依赖项。
    • @rafa.pereira 请参阅gganimate 文档。 github 上的 README (github.com/dgrtwo/gganimate) 甚至有一个间隔选项的例子来加速动画。
    猜你喜欢
    • 2016-01-06
    • 1970-01-01
    • 2014-04-16
    • 2012-06-25
    • 2016-10-18
    • 2015-01-15
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多