I have a problem animating plots where data in some layers in present only in some of the frames. In the example below, I have a moving point that can be nicely animated along 9 frames. However, when I add another layer with a point present only in some of the frames, I get the following error:
Error: time data must be the same class in all layers
Example:
require(data.table)
require(ggplot2)
require(gganimate)
# 9 points along x=y; present at every time point
dtP1 = data.table(x = 1:9,
y = 1:9,
t = 1:9)
# 3 points along x = 10-y; present at time points 2, 5, 8
dtP2 = data.table(x = c(1, 5, 9),
y = c(9, 5, 1),
t = c(2, 5, 8))
p = ggplot() +
geom_point(data = dtP1,
aes(x = x,
y = y),
color = "#000000") +
geom_point(data = dtP2,
aes(x = x,
y = y),
color = "#FF0000") +
gganimate::transition_time(t) +
gganimate::ease_aes('linear')
pAnim = gganimate::animate(p,
renderer = av_renderer("~/test.mp4"),
fps = 1,
nframes = 9,
height = 400, width = 400)