Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

r - Control speed of a gganimation

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)

toofast

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)

mostlyblank

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

enter image description here

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...