Skip to content

Instantly share code, notes, and snippets.

@z3tt
Last active December 27, 2020 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save z3tt/132ad37f368bec3ff2f872e58385fa2b to your computer and use it in GitHub Desktop.
Save z3tt/132ad37f368bec3ff2f872e58385fa2b to your computer and use it in GitHub Desktop.
gganimate problem
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())
@z3tt
Copy link
Author

z3tt commented Dec 27, 2020

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 of year == 2020 and NA_character_ otherwise.

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),
      ## added to avoid labels for all other data points instead of filtering for year 2020 inside geom_text()
      name_lab = if_else(year == 2020, name, NA_character_)  
    ) %>% 
    ungroup()
}

df_mac_index_refs <- map_df(2000:2020, ~ index_it(.x))

index_plot <- 
  ggplot(df_mac_index_refs %>% filter(group != "other"), 
         aes(year, usd_raw_rel, group = iso_a3)) + 
  geom_line(
    data = df_mac_index_refs %>% filter(group == "other"),
    color = "grey87"
  ) +
  geom_line(aes(color = group)) +
  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 = 6, name = "Bold")[1:5]) +
  ggrepel::geom_text_repel(
    aes(color = group,
          label = name_lab),
    family = "Avenir Next Condensed",
    fontface = "bold",
    size = 4,
    direction = "y",
    xlim = c(2021, NA),
    linetype = "dotted"
  ) +
  transition_time(ref_year) +
  ease_aes('quadratic-in-out')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment