使用transition_reveal在gganimate中以特定帧/时间点暂停的任何方法?

Vic*_*ica 12 r gganimate

利用Github上包的wiki中的这个例子:

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))
Run Code Online (Sandbox Code Playgroud)

产生类似的东西:

在此输入图像描述

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

编辑:包工作者提到它可能在Twitter上,但我不知道他所指的是什么'揭示时间'论点.

Jon*_*ing 15

来自OP:

编辑:包工作者提到它可能在Twitter上,但我不知道他所指的是什么'揭示时间'论点.

在Twitter上,Thomas Lin Pedersen指的是这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()
Run Code Online (Sandbox Code Playgroud)

然后我将其输入动画,更改源数据框和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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但是当我这样做时,我意识到它并没有暂停 - 它只是放慢了补间的速度.排序的"子弹时间"效果 - 很酷,但不是我想要的.

所以我的第二种方法是实际复制动画的暂停行.通过这样做,将没有补间,并且会有真正的暂停:

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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述