首页 > 解决方案 > 使用 ggplot 和 gganimate 制作动画

问题描述

我正在尝试使用 来学习动画情节gganimate,我想知道是否有人对我遇到的问题有提示。为了使事情变得简单,我通过在 RStudio Cloud 中创建一个新项目,安装ggplot2gganimatedatasauRus包,并遵循Isaac Faber的这个示例来做到这一点:

library(datasauRus)
library(ggplot2)
library(gganimate)
ggplot(datasaurus_dozen, aes(x=x,y=y)) +
  geom_point() + 
  theme_minimal() + 
  transition_states(dataset,3,1) +
  ease_aes()

这会创建一系列 .PNG 文件,但我看不到动画。有些人似乎建议我可以使用“打印”功能看到它,但这也不起作用。

尽管我遵循了此处给出的建议,但我也无法将其导出为 .GIF 。具体来说,这些magick包对我不起作用(我收到关于我的图像不是魔法图像对象的相同错误),并且当我尝试以下代码时:

p <- ggplot(datasaurus_dozen, aes(x=x,y=y)) +
  geom_point() + 
  theme_minimal() + 
  transition_states(dataset,3,1) +
  ease_aes()
anim <- animate(p)
anim_save("myfilename.gif",anim)

R告诉我

动画对象没有指定 save_animation 方法。

我一直无法找到告诉我如何指定 save_animation 方法的示例或文档。如果有人对此主题有任何建议,将不胜感激!

标签: rggplot2gganimate

解决方案


你做的太多了:

library(datasauRus) 
library(ggplot2) 
library(gganimate)

p <- ggplot(datasaurus_dozen, aes(x=x,y=y)) +
geom_point() +
theme_minimal() +
transition_states(dataset,3,1) + 
ease_aes() 

anim_save("myfilename.gif",p)

在此处输入图像描述


推荐阅读