【问题标题】:Animation time with gganimategganimate 的动画时间
【发布时间】:2022-01-27 11:46:35
【问题描述】:

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

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

【问题讨论】:

  • 嗨@Miguel Angel Acosta Chinchilla。你的动画在我的电脑上运行良好。因此,不确定是否完全理解您的要求。干杯。
  • @lovalery,我需要点出现和消失,但在同一位置,而不是像我的代码那样移动。
  • 好的。非常感谢您的反馈。所以@Kat 的答案似乎就是你要找的。干杯。

标签: r tidyverse gganimate


【解决方案1】:

如果您不想按行或按变量设置动画,只需一次,您可以使用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

【讨论】:

  • 这非常适合我的要求。我试着找到这个动画,谢谢
  • @Kat,每个圆圈类型出现的存在形式。例如第一个点a 和下一个b。在不同的时间流逝。但在最后的所有点。为了更好地解释数据。
  • 对不起。我不明白你在评论中想说什么。您能否尝试以另一种方式解释它或提供您想要的示例? (即使你从某处复制它。那个“某处”也不必在 R 中有一个起源。)
  • 好吧,对不起,我希望图表首先显示 cl a 的圆圈,然后是 cl b ,由于每个都有不同的颜色,因此可以更好地欣赏。最后,如果同时看到 ab。因为这会同时显示所有圈子。咳咳!
  • 我更新了我的答案。如果还有其他事情,请告诉我。
猜你喜欢
  • 1970-01-01
  • 2020-07-23
  • 2022-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-13
相关资源
最近更新 更多