Last active
December 27, 2020 19:16
-
-
Save z3tt/132ad37f368bec3ff2f872e58385fa2b to your computer and use it in GitHub Desktop.
gganimate problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(gganimate) | |
library(systemfonts) | |
df_mac_raw <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-12-22/big-mac.csv') | |
df_mac <- | |
df_mac_raw %>% | |
mutate(year = lubridate::year(date)) %>% | |
dplyr::select(date, year, iso_a3, currency_code, name, local_price, | |
dollar_ex, dollar_price, usd_raw) %>% | |
group_by(iso_a3, name, year) %>% | |
summarize(usd_raw = mean(usd_raw)) %>% | |
group_by(iso_a3) %>% | |
filter(n() == 21) | |
highlights <- c("EUZ", "CHE", "DNK", "SWE") ## Europe | |
index_it <- function(y) { | |
countries <- | |
df_mac %>% | |
filter(year == y) %>% | |
pull(iso_a3) | |
df_mac_indexed <- | |
df_mac %>% | |
group_by(iso_a3) %>% | |
filter(iso_a3 %in% countries) %>% | |
mutate( | |
ref_year = y, | |
usd_raw_index = usd_raw[which(year == y)], | |
usd_raw_rel = usd_raw - usd_raw_index, | |
group = if_else(iso_a3 %in% highlights, iso_a3, "other"), | |
group = as.factor(group) | |
) %>% | |
mutate( | |
group = fct_relevel(group, "other", after = Inf) | |
) %>% | |
ungroup() | |
} | |
df_mac_index_refs <- map_df(2000:2020, ~ index_it(.x)) | |
index_plot <- | |
ggplot(df_mac_index_refs, | |
aes(year, usd_raw_rel, group = iso_a3)) + | |
geom_line( | |
data = df_mac_index_refs %>% filter(group == "other"), | |
color = "grey87" | |
) + | |
geom_line( | |
data = df_mac_index_refs %>% filter(group != "other"), | |
aes(color = group) | |
) + | |
ggrepel::geom_text_repel( | |
data = df_mac_index_refs %>% filter(group != "other", year == 2020), | |
aes(color = group, | |
label = glue::glue("{name} ({iso_a3})")), | |
family = "Avenir Next Condensed", | |
fontface = "bold", | |
size = 4, | |
hjust = 0, | |
nudge_y = 1.5, | |
direction = "y" | |
) + | |
scale_x_continuous(limits = c(2000, 2025), breaks = seq(2000, 2020, by = 5)) + | |
scale_y_continuous(limits = c(-1, 1)) + | |
scale_color_manual(values = rcartocolor::carto_pal(n = 5, name = "Bold")[1:4]) + | |
transition_time(ref_year) + | |
ease_aes('quadratic-in-out') | |
anim <- | |
animate(index_plot, nframes = 100, fps = 5, detail = 5, | |
width = 1200, height = 800, #device = "jpeg", type = "cairo", | |
renderer = magick_renderer()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Seems this is indeed related to the top-level data set and filtering it later (no clue why since all relevant information is still contained). Instead of filtering for the latest year to add the direct labels, I add a
name_lab
column where it's the name in case ofyear == 2020
andNA_character_
otherwise.