/2020-week38/kids.R

https://github.com/gkaramanis/tidytuesday · R · 56 lines · 44 code · 5 blank · 7 comment · 4 complexity · 902e698f6803e8b2c2f628612b6a7a7c MD5 · raw file

  1. library(tidyverse)
  2. library(geofacet)
  3. library(colorspace)
  4. kids <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-09-15/kids.csv')
  5. states_abbr <- read_csv(here::here("2020-week38", "data", "us-states-abbr-AP.csv")) %>%
  6. select(state = title_case, ap_style)
  7. libraries <- kids %>%
  8. filter(variable == "lib") %>%
  9. filter(year == 1997 | year == 2016) %>%
  10. mutate(inf_adj_perchild = inf_adj_perchild * 1000) %>%
  11. pivot_wider(id_cols = state, names_from = year, names_prefix = "year_", values_from = inf_adj_perchild) %>%
  12. mutate(diff = year_2016 - year_1997) %>%
  13. left_join(states_abbr)
  14. f1 <- "Proxima Nova"
  15. f1b <- "Proxima Nova Bold"
  16. f1m <- "Proxima Nova Medium"
  17. f2c <- "IBM Plex Sans Condensed"
  18. ggplot(libraries) +
  19. # Point and arrow
  20. geom_point(aes(x = 0, y = year_1997, color = ifelse(diff > 0, "grey97", "grey20")), size = 1.5) +
  21. geom_segment(aes(x = 0, xend = 1, y = year_1997, yend = year_2016, color = ifelse(diff > 0, "grey97", "grey20")), arrow = arrow(length = unit(0.2, "cm")), size = 0.75) +
  22. # State label
  23. geom_text(aes(x = -0.3, y = 350, label = ap_style), stat = "unique", hjust = 0, color = "grey95", family = f1b, size = 3.5) +
  24. # 1997 value label
  25. geom_text(aes(x = 0, y = year_1997 - 90, label = round(year_1997)), hjust = 0.5, family = f2c, size = 3, color = darken("#7E95A9", 0.3)) +
  26. # 2016 value label
  27. geom_text(aes(x = 1.1, y = year_2016 - 90, label = round(year_2016)), hjust = 0.5, family = f2c, size = 3, color = darken("#7E95A9", 0.4)) +
  28. # Scales, facet, labs
  29. # scale_color_gradient2(low = "red", mid = "grey75", high = "grey97") +
  30. scale_color_identity() +
  31. facet_geo(vars(state)) +
  32. labs(
  33. title = "Change in public spending on libraries from 1997 to 2016",
  34. subtitle = "Dollars spent per child, adjusted for inflation",
  35. caption = "Source: Urban Institute | Graphic: Georgios Karamanis"
  36. ) +
  37. coord_cartesian(clip = "off", expand = FALSE) +
  38. # Theme
  39. theme_void() +
  40. theme(
  41. legend.position = "none",
  42. plot.background = element_rect(fill = "#7E95A9", color = NA),
  43. plot.margin = margin(20, 30, 15, 30),
  44. panel.spacing.x = unit(1.5, "lines"),
  45. panel.spacing.y = unit(1, "lines"),
  46. strip.text = element_blank(),
  47. plot.title = element_text(size = 16, family = f1b, hjust = 0.5, margin = margin(10, 0, 0, 0)),
  48. plot.subtitle = element_text(size = 14, family = f1, hjust = 0.5, margin = margin(5, 0, 25, 0)),
  49. plot.caption = element_text(family = f1, hjust = 1, margin = margin(20, 0, 0, 0))
  50. ) +
  51. ggsave(here::here("temp", paste0("kids-", format(Sys.time(), "%Y%m%d_%H%M%S"), ".png")), dpi = 320, width = 9, height = 8)