gganimate + 事件研究图gif

问题描述 投票:0回答:1

我有以下数据和代码。我的最终目的是重新创建我附加的 gif。我想为我创建的 ggplot 图表制作动画,然后保存 gif。我希望 t_es 上的过渡能够平滑(行进时出现的线),然后在 t_es = 0 时停止很短的时间,然后继续平滑。虽然我确实为情节设置了动画,但当 t_es = 0 时,我并没有实现它停止一点点。

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) +  # Adjust size as needed for line thickness
  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()

enter image description here

r ggplot2 gganimate
1个回答
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 <- uruguay |> 
  mutate(pause = if_else(t_es == 0, 20, 1)) |> # set pause for 20 at zero
  uncount(pause) |> # repeat pause rows
  mutate(reveal = row_number(), .by = c(name, gender)) |> # set the reveal sequence
  ggplot(aes(t_es, estimate, group = gender, color = gender)) +
  geom_vline(xintercept = uruguay$t_es[uruguay$t_es == 0], linetype = "solid", color = "black") +
  geom_line(linewidth = 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))

p + 
  transition_reveal(reveal) + # use the reveal sequence
  enter_fade() + 
  exit_fade() +
  ease_aes('linear') +
  shadow_mark()

创建于 2024-04-27,使用 reprex v2.1.0

© www.soinside.com 2019 - 2024. All rights reserved.