/2020-week38/kids-table.R

https://github.com/gkaramanis/tidytuesday · R · 104 lines · 96 code · 8 blank · 0 comment · 6 complexity · c648bf2958a7c1540ba3bcd1f1400dbf MD5 · raw file

  1. library(tidyverse)
  2. library(gt)
  3. kids <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-09-15/kids.csv')
  4. libraries <- kids %>%
  5. filter(variable == "lib") %>%
  6. select(state, year, inf_adj_perchild) %>%
  7. mutate(inf_adj_perchild = round(inf_adj_perchild * 1000))
  8. plot_group <- function(name, df){
  9. plot_object <- df %>%
  10. mutate(
  11. start_inf = if_else(year == 1997, inf_adj_perchild, NA_real_),
  12. end_inf = if_else(year == 2016, inf_adj_perchild, NA_real_)
  13. ) %>%
  14. tidyr::fill(start_inf, end_inf, .direction = "downup") %>%
  15. mutate(color = if_else(end_inf - start_inf < 0, "red", "blue")) %>%
  16. ggplot(aes(x = year, y = inf_adj_perchild,color = color)) +
  17. geom_line(size = 15) +
  18. theme_void() +
  19. scale_color_identity() +
  20. theme(legend.position = "none")
  21. return(plot_object)
  22. }
  23. sparklines <- libraries %>%
  24. group_by(state) %>%
  25. nest() %>%
  26. mutate(plot = map2(state, data, plot_group)) %>%
  27. select(-data)
  28. table_prepped <- libraries %>%
  29. filter(year == 1997 | year == 2016) %>%
  30. pivot_wider(id_cols = state, names_from = year, values_from = inf_adj_perchild) %>%
  31. mutate(change = round((`2016` - `1997` ) / `1997` * 100)) %>%
  32. inner_join(sparklines, by = "state") %>%
  33. mutate(ggplot = NA)
  34. top_55 <- table_prepped %>%
  35. filter(dense_rank(change) <= 5 | dense_rank(desc(change)) <= 5) %>%
  36. arrange(-change)
  37. top_55 %>%
  38. gt() %>%
  39. text_transform(
  40. locations = cells_body(vars(ggplot)),
  41. fn = function(x){
  42. map(top_55$plot, ggplot_image, height = px(15), aspect_ratio = 4)
  43. }
  44. ) %>%
  45. tab_row_group(
  46. group = "Bottom 5",
  47. rows = 6:10
  48. ) %>%
  49. tab_row_group(
  50. group = "Top 5",
  51. rows = 1:5
  52. ) %>%
  53. cols_label(
  54. ggplot = "1997-2016",
  55. state = "State",
  56. change = "% Change"
  57. ) %>%
  58. tab_style(
  59. style = cell_text(font = "Proxima Nova"),
  60. locations = cells_body(columns = 1)
  61. ) %>%
  62. tab_style(
  63. style = cell_text(font = "IBM Plex Sans Condensed"),
  64. locations = cells_body(columns = 2:4)
  65. ) %>%
  66. tab_style(
  67. style = cell_text(font = "Graphik Compact", weight = "bold"),
  68. locations = cells_column_labels(columns = gt::everything())
  69. ) %>%
  70. tab_style(
  71. style = cell_text(font = "Graphik Compact"),
  72. locations = cells_title()
  73. ) %>%
  74. tab_style(
  75. style = cell_text(font = "Graphik Compact"),
  76. locations = cells_row_groups()
  77. ) %>%
  78. tab_header(
  79. title = "Change in public spending on libraries",
  80. subtitle = "Dollars spent per child, adjusted for inflation"
  81. ) %>%
  82. tab_source_note("Source: Urban Institute, Table: Georgios Karamanis") %>%
  83. tab_options(
  84. row_group.border.top.width = px(5),
  85. row_group.border.top.color = "white",
  86. row_group.border.bottom.width = px(1),
  87. row_group.border.bottom.color = "lightgrey",
  88. table.border.top.color = "white",
  89. table.border.top.width = px(5),
  90. table.border.bottom.color = "white",
  91. column_labels.border.bottom.color = "white",
  92. column_labels.border.bottom.width = px(2),
  93. column_labels.border.top.width = px(10),
  94. column_labels.border.top.color = "white"
  95. ) %>%
  96. cols_hide(vars(plot))