26

我想在使用library(gganimate).

这是一个小例子:

# 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)

太快

我的理想行为会慢得多(10-100x),所以颜色变化逐渐演变,没有人癫痫发作。

如果我尝试使用transition_states()更多的手动控制,我会得到一个带有大部分空白帧的 gif。我尝试了各种组合transition_legnth=state_length=但没有明显效果。

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)

大部分为空白

4

1 回答 1

35

我在docs animate函数中找到了可以采用fpsdetail参数的函数。

@param fps动画的帧速率,以帧/秒为单位

@param detail每帧要计算的附加帧数

结果:

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)

在此处输入图像描述

您还可以在那里指定输出格式,例如pngjpegsvg

于 2018-09-09T18:12:41.120 回答