gganimate 的动画时间

Animation time with gganimate

我想制作动画情节。我需要点出现和消失,但只是让点移动。

set.seed(1)
library(tidyverse)
library(gganimate)
df <- tibble(
  x = rnorm(100)
  , y = rnorm(100)
  , size = rep(c(2, 3, 4, 5), 25)
  , cl = sample(c("a", "b"), 100, T)
  , time = rep(1:10, 10) #|> lubridate::year()
)
p2 <-
  df |>
  ggplot() +
  aes(x, y, size = size, color = cl) +
  geom_point() +
  scale_size(range = c(5, 12)) +
  transition_reveal(time) +
  #enter_fade()
  #transition_manual(time) +
  shadow_mark(past = F, future = T)
animate(p2, renderer = gifski_renderer("sa.gif"))

如果您不想一次按行或按变量设置动画,您可以使用任何 enter 状态和 transition_layers。它不会显示最终的过渡,但只会为入口设置动画。我添加了 enter_fade() 这样您就可以看到入口了。

p2 <-
  df |>
  ggplot() +
  aes(x, y, size = size, color = cl) +
  geom_point() +
  scale_size(range = c(5, 12)) +
  theme_bw() +
  transition_layers(layer_length = 2, 
                    transition_length = 2) +
  enter_fade() 

更新:


在您的评论中,您询问是否可以先将组 a,然后再组 b。我想这就是您要找的。

p3 <- ggplot() +
  geom_point(data = filter(df, cl == "a"),
             aes(x, y, color = cl, size = size)) + 
  geom_point(data = filter(df, cl == "b"),
             aes(x, y, color = cl, size = size)) +
  scale_size(range = c(5, 12)) +
  theme_bw() +
  transition_layers(layer_length = 2, 
                    transition_length = 2) +
  enter_fade()

p3