Control speed of a gganimation

2020-02-05 18:40发布

问题:

I want to slow the transition speed between states when using library(gganimate).

Here is a mini example:

# devtools::install_github("thomasp85/gganimate")
library(gganimate) # v0.9.9.9999

dat_sim <- function(t_state, d_state) {
  data.frame(
    x = runif(1000, 0, 1),
    y = runif(1000, 0, 1),
    t_state = t_state*d_state
    )
}

dat <- purrr::map_df(1:100, ~ dat_sim(., 1))

ggplot(dat, aes(x, y)) +
  geom_hex(bins = 5) +
  theme_void() +
  lims(x = c(.3, .7),
       y = c(.3, .7)) +
  theme(legend.position = "none") +
  transition_time(t_state)

My ideal behavior would be much slower (10-100x), so color changes gradually evolve and nobody has a seizure.

If I try to use transition_states() for more manual control, I get a gif with mostly blank frames. I've tried various combinations for transition_legnth= and state_length= without a noticeable effect.

ggplot(dat, aes(x, y)) +
  geom_hex(bins = 5) +
  theme_void() +
  lims(x = c(.3, .7),
       y = c(.3, .7)) +
  theme(legend.position = "none") +
  transition_states(t_state, transition_length = .1, state_length = 2)

回答1:

I found in docs animate function which can take fps and detail parameters.

@param fps The frame rate of the animation in frames/sec

@param detail The number of additional frames to calculate, per frame

The result:

p <- ggplot(dat, aes(x, y)) +
      geom_hex(bins = 5) +
      theme_void() +
      lims(x = c(.3, .7),
           y = c(.3, .7)) +
      theme(legend.position = "none") +
      transition_time(t_state)
animate(p, fps=1)

Also there you can specify output format such as png, jpeg, svg.