【问题标题】:Any way to pause at specific frames/time points with transition_reveal in gganimate?有什么方法可以在 gganimate 中使用 transition_reveal 在特定帧/时间点暂停?
【发布时间】:2019-04-05 03:25:09
【问题描述】:

利用 Github 上软件包 wiki 中的 this 示例:

airq <- airquality
airq$Month <- format(ISOdate(2004,1:12,1),"%B")[airq$Month]

ggplot(airq, aes(Day, Temp, group = Month)) + 
  geom_line() + 
  geom_segment(aes(xend = 31, yend = Temp), linetype = 2, colour = 'grey') + 
  geom_point(size = 2) + 
  geom_text(aes(x = 31.1, label = Month), hjust = 0) + 
  transition_reveal(Month, Day) + 
  coord_cartesian(clip = 'off') + 
  labs(title = 'Temperature in New York', y = 'Temperature (°F)') + 
  theme_minimal() + 
  theme(plot.margin = margin(5.5, 40, 5.5, 5.5))

产生类似的东西:

我想知道是否有任何方法可以在动画的特定点定义暂停。例如在第 10 天,然后是 20 天,然后当动画完成时,再次循环之前。 geom_reveal 没有可用的 state_lengthtransition_length 参数,所以我不确定这是否可行。

编辑:包作者在twitter 上提到它是可能的,但我不知道他指的是什么“揭示时间”论点。

【问题讨论】:

    标签: r gganimate


    【解决方案1】:

    来自 OP:

    编辑:包作者提到有可能[这样做],但我没有 知道他指的是什么“揭示时机”论点。

    在 Twitter 上,Thomas Lin Pedersen was referring 了解 transition_reveal 行如何驱动动画帧。所以我们可以给它一个变量作为动画的“心跳”,而将原始变量留给情节。

    我的第一个方法是创建一个新变量reveal_time,这将是心跳。我会在暂停点增加更多,这样动画就会在这些数据点上花费更多时间。在这里,我通过在暂停点天添加 10 个来做到这一点,而在其他天只添加 1 个。

    library(dplyr)
    airq_slowdown <- airq %>%
      group_by(Month) %>%
      mutate(show_time = case_when(Day %in% c(10,20,31) ~ 10,
                                         TRUE           ~ 1),
             reveal_time = cumsum(show_time)) %>%
      ungroup()
    

    然后我将它输入到动画中,更改源数据框和 transition_reveal 行。

    library(gganimate)
    a <- ggplot(airq_slowdown, aes(Day, Temp, group = Month)) + 
      geom_line() + 
      geom_segment(aes(xend = 31, yend = Temp), linetype = 2, colour = 'grey') + 
      geom_point(size = 2) + 
      geom_text(aes(x = 31.1, label = Month), hjust = 0) + 
      transition_reveal(reveal_time) +  # Edit, previously had (Month, reveal_time)
      coord_cartesian(clip = 'off') + 
      labs(title = 'Temperature in New York', y = 'Temperature (°F)') + 
      theme_minimal() + 
      theme(plot.margin = margin(5.5, 40, 5.5, 5.5))    
    animate(a, nframe = 50)
    

    但是当我这样做时,我意识到它并没有暂停——它只是减慢了补间。有点像“子弹时间”的效果——很酷,但不是我想要的。

    所以我的第二种方法是实际复制动画的暂停行。这样做,不会有补间,会有真正的停顿:

    airq_pause <- airq %>%
      mutate(show_time = case_when(Day %in% c(10,20,31) ~ 10,
                                   TRUE           ~ 1)) %>%
      # uncount is a tidyr function which copies each line 'n' times
      uncount(show_time) %>%
      group_by(Month) %>%
      mutate(reveal_time = row_number()) %>%
      ungroup()
    

    【讨论】:

    • Tweet [twitter.com/sobradob/status/1177691643627094023] 注意到当前错误(gganimate 的 1.0.3.9000 版本。将transition_reveal 行更新为transition_reveal(reveal_time),现在似乎可以使用。不确定原始答案是否有错字或如果 API 略有变化。
    【解决方案2】:

    您可以使用 animate 函数的end_pause 参数:

    library(gganimate)
    library(animation)
    
    animation <- barplot +  transition_reveal(anho)
    #barplot built in ggplot2
    
    animate(animation, end_pause = 10, width=1000, height=600,fps = 5)
    

    【讨论】:

    • 你可以添加一段解释你的代码。
    • 这会在循环重新开始之前的最后一帧暂停。不是 OP 要求的
    猜你喜欢
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 2020-02-14
    • 2022-09-23
    • 1970-01-01
    • 2014-12-08
    • 2017-02-22
    • 1970-01-01
    相关资源
    最近更新 更多