jjm*_*elo 1 r ggplot2 gganimate
我正在尝试使用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")
Run Code Online (Sandbox Code Playgroud)
使用的数据文件在这里。
最初我使用 geom_lines 而不是 geom_point,但这产生了一个错误:
Error in seq.default(range[1], range[2], length.out = nframes) :
'from' must be a finite number
Run Code Online (Sandbox Code Playgroud)
和
Error in transform_path(all_frames, next_state, ease, params$transition_length[i], :
transformr is required to tween paths and lines
Run Code Online (Sandbox Code Playgroud)
要么不喜欢台词,要么不喜欢台词。切换到点,并按照 gganimate 问题中的建议创建文件。但是,这会产生不同类型的错误:
Error: Provided file does not exist
Run Code Online (Sandbox Code Playgroud)
我真的无法弄清楚,因为我根本没有提供任何文件。无论如何试图保存会产生
Error: The animation object does not specify a save_animation method
Run Code Online (Sandbox Code Playgroud)
所以我真的不知道我是否做错了什么,使用了不推荐使用的版本(或包)或什么。
使用的版本
问题似乎是使用{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')
Run Code Online (Sandbox Code Playgroud)
或者(这更流畅一点):
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}')
Run Code Online (Sandbox Code Playgroud)
theme电话,因为它不相关> packageVersion('ggplot2')
[1] ‘3.3.0’
> packageVersion('gifski')
[1] ‘0.8.6’
> packageVersion('gganimate')
[1] ‘1.0.5’
Run Code Online (Sandbox Code Playgroud)