How to animate points being added to a plot in a certain amount of time?

Viewed 118

I have a df containing 10000 data points that I want to add to a plot one by one using gganimate. So the data is:

df = data.frame(x = runif(10000),
                y = runif(10000),
                z = 1:10000) # z column is needed for transition_states()

I created the animation using:

library(ggplot2)
library(gganimate)

ggplot(df, aes(x, y)) +
        geom_point() +
        transition_states(z) +
        shadow_mark() +
        labs(title = "Point num: {closest_state}")

But I struggle to get all of my points shown in the animation. For example, I would like to show the points from 1 to 10000 to be added in the animation in about 60-100 seconds.

1 Answers

You can use the duration parameter in the animate function.

anim <- ggplot(df, aes(x, y)) +
  geom_point() +
  transition_states(z, state_length = 0) +
  shadow_mark() +
  labs(title = "Point num: {closest_state}"

animate(anim, duration = 80, fps = 10000/80, renderer = gifski_renderer())

Since you have a lot of points in your dataset, I would suggest to group your point so that you don't end up with 10,000 frames, which takes a lot of time to compute!

Related