Search code examples
rggplot2gganimate

gganimate + event study graph gif


I have the following data and code. My ultimate purpose is to recreate the gif I am attaching. I want to animate the ggplot graph that I created and then, save a gif. I want the transition to be smooth on t_es (the line appearing as it advances) and then, stop for a really short time when t_es = 0, and then keep going smooth. While I do animate the plot, I am not achieving it to stop a tiny bit when 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


Solution

  • The added/changed lines are commented:

    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) + # changed from size (deprecated)
      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()
    

    Created on 2024-04-27 with reprex v2.1.0