【问题标题】:Using gganimate and getting all kind of errors使用 gganimate 并得到各种错误
【发布时间】:2020-07-04 08:14:17
【问题描述】:

我正在尝试使用gganimate 创建动画:

library(ggplot2)
library(ggthemes)
library(gifski)
library(gganimate)

load("covid-19-es.Rda")
casos <- ggplot(data,aes(x=Fecha))+geom_point(aes(y=casos,color="Casos"))+geom_point(aes(y=salidas,color="Salidas"))+theme_tufte()+transition_states(Fecha,transition_length=2,state_length=1)+labs(title='Day: {frame_time}')
animate(casos, duration = 5, fps = 20, width =800, height = 600, renderer=gifski_renderer())
anim_save("casos.png")

使用的数据文件是here

最初我使用 geom_lines 而不是 geom_point,但这会产生错误提示:

Error in seq.default(range[1], range[2], length.out = nframes) : 
  'from' must be a finite number

Error in transform_path(all_frames, next_state, ease, params$transition_length[i],  : 
  transformr is required to tween paths and lines

它要么不喜欢线条,要么不喜欢其中的一对。切换到点,并按照 gganimate 问题中的建议创建文件。但是,这会产生不同类型的错误:

Error: Provided file does not exist

我真的无法弄清楚,因为我根本没有提供任何文件。无论如何尝试保存都会产生

Error: The animation object does not specify a save_animation method

所以我真的不知道我是否做错了什么,使用了已弃用的版本(或包)或什么。

使用的版本

  • R 3.6
  • ggplot 2_3.3.0
  • gganimate 1.0.5
  • gifski 0.8.6

【问题讨论】:

  • 很好奇您想要动画的样子,因为您在 x 轴上有日期并将其用作构面变量。你想随着时间的推移显示线路吗?在这种情况下,您可能需要transition_reveal()
  • @paqmo 是的,那是我的意图......

标签: r ggplot2 gganimate


【解决方案1】:

问题似乎是{frame_time} 的使用。

如果您拨打transition_states,请使用{closest_state} 进行状态跟踪。或者,致电transition_time 并使用{frame_time}

这对我有用,使用 transition_states{closest_state}

library(ggplot2)
library(gifski)
library(gganimate)

load("covid-19-es.Rda")

my_plot <- ggplot(data,aes(x = Fecha)) + 
    geom_point(aes( y =casos, color = "Casos")) +
    geom_point(aes(y = salidas, color = "Salidas")) +
    transition_states(Fecha, transition_length = 2, state_length = 1) +
    labs(title = 'Day: {closest_state}')

animate(
    plot = my_plot,
    render = gifski_renderer(),
    height = 600,
    width = 800, 
    duration = 5,
    fps = 20)

anim_save('my_gif.gif')

或者(这更平滑一些):

my_plot <- ggplot(data,aes(x = Fecha)) + 
    geom_point(aes( y =casos, color = "Casos")) +
    geom_point(aes(y = salidas, color = "Salidas")) +
    transition_time(Fecha) +
    labs(title = 'Day: {frame_time}')
  • 我删除了 theme 调用,因为它不相关
  • 我保存为 gif 而不是 png
  • 我使用的包版本如下:
> packageVersion('ggplot2')
[1] ‘3.3.0’
> packageVersion('gifski')
[1] ‘0.8.6’
> packageVersion('gganimate')
[1] ‘1.0.5’
  • 我正在使用 R-3.6.3。

【讨论】:

  • 那行得通。然而,唯一的区别似乎是主题。是这样吗?
  • 主题无关紧要;我刚刚编辑了帖子,提供了有关该问题的更多信息,希望对您有所帮助。
猜你喜欢
  • 2011-02-27
  • 2014-04-10
  • 2015-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-02
相关资源
最近更新 更多