I'm making a gganimate object, and would like points from one geom_point to transition, and points from another geom_point object (which has less data) to stay for the whole video.
gg_df <-
tibble(
frame = 1:100,
x = 1:100,
y = 1:100,
object = rep(c('player_a', 'player_b'), 50)
)
gg_bounces <-
tibble(
frame = seq(1, 100, 10),
x = seq(100, 1, -10),
y = seq(1, 100, 10)
)
p <-
ggplot() +
geom_point(data = gg_df, aes(x, -y, col = object)) +
geom_point(data = gg_bounces, aes(x, -y), fill = 'red', col = 'red', size = 5) +
transition_reveal(frame)
b <- animate(p, duration = 15, fps = 25, renderer = av_renderer(), height = 1000, width = 600, res = 170)
anim_save("output.mp4", b)
Assuming that you want to display the points from gg_bounces
in each frame of the animation you have to drop or rename the frame
column in this dataset.
Note: As is your example data and code did not work because of the different data types. That's why I convert the columns in gg_bounces
to integers.
library(ggplot2)
library(gganimate)
gg_bounces[] <- lapply(gg_bounces, as.integer)
gg_bounces$frame <- NULL
ggplot() +
geom_point(data = gg_df, aes(x, -y, col = object)) +
geom_point(data = gg_bounces, aes(x, -y), col = "red", size = 5) +
transition_reveal(frame)
EDIT To show the points from gg_bounces
too but keep them until the end of the animation we could follow the example in the docs and assign a unique group using group = seq_along(frame)
(of course do we have to keep the frame
column in this case as well):
ggplot() +
geom_point(data = gg_df, aes(x, -y, col = object)) +
geom_point(
data = gg_bounces, aes(x, -y, group = seq_along(frame)),
col = "red", size = 5
) +
transition_reveal(frame)