问题
I am trying to create an animation using 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")
The data file used is here.
Initially I used geom_lines instead of geom_point, but that produced an error saying:
Error in seq.default(range[1], range[2], length.out = nframes) :
'from' must be a finite number
and
Error in transform_path(all_frames, next_state, ease, params$transition_length[i], :
transformr is required to tween paths and lines
Either it does not like lines, or does not like couples of them. Switched to point, and followed advice in gganimate issues to create a file. However, that produces different kind of errors:
Error: Provided file does not exist
which I really can't figure out, since I simply didn't provide any file. Trying to save anyway produces
Error: The animation object does not specify a save_animation method
So I don't really know if I'm doing something wrong, using a deprecated version (or package) or what.
Versions used
- R 3.6
- ggplot 2_3.3.0
- gganimate 1.0.5
- gifski 0.8.6
回答1:
The issue seems to be the use of {frame_time}
.
If you're calling transition_states
, use {closest_state}
for your state tracking. Alternatively, call transition_time
and use {frame_time}
.
This works for me, using transition_states
and {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')
Alternatively (and this is a bit smoother):
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}')
- I've removed the
theme
call because it's not relevant - I've saved as a gif instead of png
- The package versions I'm using are as follows:
> packageVersion('ggplot2')
[1] ‘3.3.0’
> packageVersion('gifski')
[1] ‘0.8.6’
> packageVersion('gganimate')
[1] ‘1.0.5’
- I'm using R-3.6.3.
来源:https://stackoverflow.com/questions/60820794/using-gganimate-and-getting-all-kind-of-errors