首页 > 解决方案 > 使用 gganimate 导出 gif

问题描述

该包gganimate创建 gifs(来自此处的 MWE 代码):

    library(ggplot2)
    #devtools::install_github('thomasp85/gganimate')
    library(gganimate)

    p <- ggplot(mtcars, aes(factor(cyl), mpg)) + 
            geom_boxplot() + 
            # Here comes the gganimate code
            transition_states(
                    gear,
                    transition_length = 2,
                    state_length = 1
            ) +
            enter_fade() + 
            exit_shrink() +
            ease_aes('sine-in-out')

现在怎么能导出这个gif?在以前的(现在存档的)版本中,gganimate这很简单:

    gganimate(p, "output.gif")

gganimate但是,我在当前包中找不到等效功能。


注意:这个问题似乎与我从中获取 MWE 代码的问题完全相同。但是,gganimate已更新,在新版本中,在查看器窗格中显示动画与导出动画似乎是不同的问题。

标签: rggplot2gganimate

解决方案


gganimate 1.0.6 和 gifski 0.8.6

根据@Ronak Shah 的建议,我使用anim_save()from the package 添加了一个更新的答案 - 因为它现在gganimate使用 gifski来呈现输出。.gif

library(ggplot2)
library(gganimate)
# install.package("gifski") #if not already installed

p <- ggplot(mtcars, aes(factor(cyl), mpg)) + 
  geom_boxplot() + 
  transition_states(
    gear,
    transition_length = 2,
    state_length = 1
  ) +
  enter_fade() + 
  exit_shrink() +
  ease_aes('sine-in-out')

anim_save("filenamehere.gif", p)

在此处输入图像描述


推荐阅读