有什么方法可以在 gganimate 中使用 transition_reveal 在特定的 frames/time 点暂停?
Any way to pause at specific frames/time points with transition_reveal in gganimate?
利用 Github 上软件包维基中的 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_length
或 transition_length
参数,所以我不确定这是否可行。
编辑:包的作者在 twitter 上提到它是可能的,但我不知道他指的是什么 'reveal timing' 论点。
来自 OP:
Edit: the package author mentions it's possible [to do this] but I don't
know what 'reveal timing' argument he is referring to.
在 Twitter 上,Thomas Lin Pedersen was referring transition_reveal
线如何驱动动画帧。所以我们可以给它一个变量作为动画的 "heartbeat",同时为绘图保留原始变量。
我的第一个方法是创建一个新变量,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)
但是当我这样做时,我意识到它并没有暂停——它只是减慢了补间。有点 "bullet time" 效果 -- 很酷,但不是我想要的。
所以我的第二种方法是实际复制动画的暂停行。通过这样做,不会有补间并且会有真正的停顿:
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()
您可以使用 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)
利用 Github 上软件包维基中的 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_length
或 transition_length
参数,所以我不确定这是否可行。
编辑:包的作者在 twitter 上提到它是可能的,但我不知道他指的是什么 'reveal timing' 论点。
来自 OP:
Edit: the package author mentions it's possible [to do this] but I don't know what 'reveal timing' argument he is referring to.
在 Twitter 上,Thomas Lin Pedersen was referring transition_reveal
线如何驱动动画帧。所以我们可以给它一个变量作为动画的 "heartbeat",同时为绘图保留原始变量。
我的第一个方法是创建一个新变量,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)
但是当我这样做时,我意识到它并没有暂停——它只是减慢了补间。有点 "bullet time" 效果 -- 很酷,但不是我想要的。
所以我的第二种方法是实际复制动画的暂停行。通过这样做,不会有补间并且会有真正的停顿:
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()
您可以使用 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)