You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在gganimate动画中实现t_es=0处短暂停顿与平滑过渡

在ggplot动画的指定位置添加停顿

需要重现目标GIF动画,要求动画随t_es平滑推进显示线条,在t_es=0处短暂停顿后继续过渡,但当前实现的动画无法做到这一点。

原始数据与代码

library(tidyverse)
library(gganimate)

uruguay <- read_csv("name,confHigh,confLow,estimate,gender,t_es
                 Uruguay,0.0236,-7e-4,0.0114,male,-5
                 Uruguay,0.0276,-0.0589,-0.0157,female,-5
                 Uruguay,0.0087,-0.0157,-0.0035,male,-4
                 Uruguay,0.03,-0.048,-0.009,female,-4
                 Uruguay,0.0257,0.0028,0.0142,male,-3
                 Uruguay,0.0287,-0.0433,-0.0073,female,-3
                 Uruguay,0,0,0,male,-2
                 Uruguay,0,0,0,female,-2
                 Uruguay,0.0113,-0.0115,-1e-4,male,-1
                 Uruguay,0.0042,-0.0602,-0.028,female,-1
                 Uruguay,0.022,1e-4,0.011,male,0
                 Uruguay,-0.2773,-0.3384,-0.3079,female,0
                 Uruguay,0.0281,0.0064,0.0173,male,1
                 Uruguay,-0.2816,-0.3413,-0.3115,female,1
                 Uruguay,0.0188,-0.003,0.0079,male,2
                 Uruguay,-0.3002,-0.3575,-0.3288,female,2
                 Uruguay,0.0188,-0.0029,0.0079,male,3
                 Uruguay,-0.3197,-0.3748,-0.3472,female,3
                 Uruguay,0.0266,0.0052,0.0159,male,4
                 Uruguay,-0.3283,-0.3821,-0.3552,female,4
                 Uruguay,0.0192,-0.0027,0.0082,male,5
                 Uruguay,-0.3554,-0.4083,-0.3818,female,5
                 Uruguay,0.019,-0.0029,0.008,male,6
                 Uruguay,-0.3527,-0.4048,-0.3787,female,6
                 Uruguay,0.0195,-0.0025,0.0085,male,7
                 Uruguay,-0.3695,-0.421,-0.3953,female,7
                 Uruguay,0.0137,-0.0089,0.0024,male,8
                 Uruguay,-0.3565,-0.407,-0.3818,female,8
                 Uruguay,0.0163,-0.0062,0.0051,male,9
                 Uruguay,-0.3636,-0.4137,-0.3887,female,9
                 Uruguay,0.0118,-0.0112,3e-4,male,10
                 Uruguay,-0.3577,-0.4076,-0.3826,female,10"
)

p <- ggplot(uruguay, aes(t_es, estimate, group = gender, color = gender)) +
  geom_vline(xintercept = uruguay$t_es[uruguay$t_es == 0], linetype = "solid", color = "black") +
  geom_line(size = 1.5) +
  scale_color_manual(values = c("#D90D0D", "#6A6654")) +
  theme_void() +
  theme(legend.position = "none") +
  labs(x = "", y = "", color = "") +
  annotate("text", x = -4,  y = .01,   label = "Varones", fontface = "bold", color = "#6A6654", vjust = -0.5) +
  annotate("text", x = -4,  y = -.03,  label = "Mujeres", fontface = "bold", color = "#D90D0D", vjust = -0.5) +
  annotate("text", x = -.5, y = -.31,  label = "-31%",    fontface = "bold", color = "#D90D0D", vjust = -0.5) +
  annotate("text", x = 10,  y = -.38,  label = "-39%",    fontface = "bold", color = "#D90D0D", vjust = -0.5) +
  theme(panel.background = element_rect(fill = "#F5F4EE"),
        plot.background  = element_rect(fill = "#F5F4EE", color = NA))

anim <- p + 
  transition_reveal(along = t_es) +
  enter_fade() + exit_fade() +
  ease_aes('linear') +
  shadow_mark()

解决方案

核心思路是调整动画的帧分布,让t_es=0对应更多帧,从而实现停顿。以下两种方法均可实现:

方法1:手动扩展数据(直观易控)

复制t_es=0的行,让动画在该位置生成更多帧,自然形成停顿。

# 复制t_es=0的行5次(可根据需要调整次数,次数越多停顿越久)
uruguay_pause <- uruguay %>%
  bind_rows(replicate(5, filter(., t_es == 0), simplify = FALSE) %>% bind_rows()) %>%
  arrange(t_es, gender) # 保持数据排序正确

# 重新生成动画
anim <- ggplot(uruguay_pause, aes(t_es, estimate, group = gender, color = gender)) +
  geom_vline(xintercept = 0, linetype = "solid", color = "black") +
  geom_line(size = 1.5) +
  scale_color_manual(values = c("#D90D0D", "#6A6654")) +
  theme_void() +
  theme(legend.position = "none") +
  labs(x = "", y = "", color = "") +
  annotate("text", x = -4,  y = .01,   label = "Varones", fontface = "bold", color = "#6A6654", vjust = -0.5) +
  annotate("text", x = -4,  y = -.03,  label = "Mujeres", fontface = "bold", color = "#D90D0D", vjust = -0.5) +
  annotate("text", x = -.5, y = -.31,  label = "-31%",    fontface = "bold", color = "#D90D0D", vjust = -0.5) +
  annotate("text", x = 10,  y = -.38,  label = "-39%",    fontface = "bold", color = "#D90D0D", vjust = -0.5) +
  theme(panel.background = element_rect(fill = "#F5F4EE"),
        plot.background  = element_rect(fill = "#F5F4EE", color = NA)) +
  transition_reveal(along = t_es) +
  enter_fade() + 
  exit_fade() +
  ease_aes('linear') +
  shadow_mark()

# 保存GIF,调整fps和duration控制整体速度
anim_save("uruguay_animation.gif", anim, fps = 10, duration = 8)

方法2:使用transition_states自定义停留时间

通过state_length参数为t_es=0设置更长的停留时间,无需修改原始数据:

anim <- p +
  transition_states(t_es, 
                   transition_length = 1, # 状态间过渡的时间长度
                   state_length = ifelse(t_es == 0, 2, 0.5)) + # t_es=0停留2单位时间,其他0.5
  enter_reveal() +
  ease_aes('linear') +
  shadow_mark()

内容的提问来源于stack exchange,提问作者Paula

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 21:01:00