Quantcast
Channel: WP快站
Viewing all articles
Browse latest Browse all 233

Save .gif with gganimate respecting the original ggplot

$
0
0

I have the following code which so far, works fine. The problem is when I try to save the animated gif. All of my tries end up losing quality of image, the text annotations get misplaced and the ratio of height and weight are not respected. How can I save the gif properly?

I've done this in the past but I don't know if the text annotations are making it more difficult or so.

pacman::p_load(gganimate, tidyverse, here)# Setting consistent dimensions for both static and animated outputsplot_width <- 12plot_height <- 7dpi_setting <- 300# Motherhood Penalties | Uruguayuruguay <- 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, 2, 1)) |> # set pause for 10 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 = -.5, linetype = "solid", color = "black", linewidth = .2) +  geom_hline(yintercept = 0, linetype = "solid", color = "black") +  geom_line(linewidth = 1.5) +  scale_color_manual(values = c("#D90D0D", "#6A6654")) +  theme_minimal(base_family = "Verdana",                base_size = 16) +  labs(x = "",       y = "",       color = "",       title = "Cambio en el empleo después de tener el primer hijo/a, %",       subtitle = "Respecto a dos años antes del nacimiento",       caption = "Fuente: elaboración propia en base a The Child Penalty Atlas, por H. Kleven et al., 2023.\nPaula Pereda Suárez | @paubgood") +  geom_text(aes(x = -4, y = 0.0100, label = "Varones"), fontface = "bold", color = "#6A6654", vjust = -.5, family = "Verdana") +  geom_text(aes(x = -4, y = -.03, label = "Mujeres"), fontface = "bold", color = "#D90D0D", vjust = -.5, family = "Verdana") +  geom_text(aes(x = -.5, y = -.31, label = "-31%"), fontface = "bold", color = "#D90D0D", vjust = -.5, family = "Verdana") +  geom_text(aes(x = 10, y = -.38, label = "-39%"), fontface = "bold", color = "#D90D0D", vjust = -.5, family = "Verdana") +  annotate("text", x = -4, y = -.39,  label = "5 años antes", fontface = "bold", color = "black",   vjust = -.5) +  annotate("text", x = 3.5, y = -.39,  label = "10 años después", fontface = "bold", color = "black",   vjust = -.5) +  theme(text = element_text(family = "Verdana"),        plot.title = element_text(face = "bold"),        legend.position = "none",        panel.grid.major = element_blank(),        panel.grid.minor = element_blank(),        axis.text = element_blank(),        panel.background = element_blank(),        plot.background  = element_rect(fill  = "#F5F4EE",                                        color = "#F5F4EE")))# Save static plotggsave("uruguay.png", dpi = dpi_setting, width = plot_width, height = plot_height)# Create and save the animated plotanim <- p +  transition_reveal(along = t_es) +  enter_fade() +  exit_fade() +  ease_aes('linear') +  shadow_mark()# Calculate the dimensions for the GIF that maintain the same aspect ratio and improve the resolutiongif_width <- 960  # Twice the pixel width for better detailgif_height <- 560  # Calculated to maintain the aspect ratioanim_save("motherhood_penalties_uy.gif", animation = anim, end_pause = 10, nframes = 150, fps = 25, width = gif_width, height = gif_height)

Viewing all articles
Browse latest Browse all 233

Trending Articles