/2020-week37/friends-table.R

https://github.com/gkaramanis/tidytuesday · R · 72 lines · 66 code · 4 blank · 2 comment · 0 complexity · 778fc90bff6edae409befa8d6a4ccf2e MD5 · raw file

  1. library(tidyverse)
  2. library(gt)
  3. friends_info <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-09-08/friends_info.csv')
  4. friends_table <- friends_info %>%
  5. select(season, episode, imdb_rating) %>%
  6. mutate(episode = paste0("E", str_pad(episode, 2, "left", "0"))) %>%
  7. pivot_wider(names_from = episode, values_from = imdb_rating) %>%
  8. mutate(season = paste0("S", str_pad(season, 2, "left", "0")))
  9. gt(friends_table) %>%
  10. tab_header(
  11. title = "FRIENDS",
  12. subtitle = "IMDB Ratings"
  13. ) %>%
  14. cols_label(
  15. season = ""
  16. ) %>%
  17. fmt_missing(
  18. columns = 2:last_col(),
  19. missing_text = ""
  20. ) %>%
  21. data_color(
  22. columns = 2:last_col(),
  23. colors = scales::col_numeric(
  24. palette = c("red", "yellow", "darkgreen"),
  25. na.color = "white",
  26. domain = c(5.5, 10)
  27. )
  28. ) %>%
  29. tab_style(
  30. cell_text(
  31. font = "IBM Plex Mono",
  32. align = "right"
  33. ),
  34. locations = list(
  35. cells_body(columns = 2:last_col())
  36. )
  37. ) %>%
  38. tab_style(
  39. cell_text(
  40. font = "Futura",
  41. weight = "bold"
  42. ),
  43. locations = list(
  44. cells_title(groups = c("title"))
  45. )
  46. ) %>%
  47. tab_style(
  48. list(
  49. cell_borders(sides = "all", color = "white", style = "solid", weight = px(1.5))),
  50. locations = list(
  51. cells_body()
  52. )
  53. ) %>%
  54. # column and row labels
  55. tab_style(
  56. cell_text(font = "IBM Plex Sans"),
  57. locations = list(
  58. cells_column_labels(gt::everything()),
  59. cells_body(columns = 1)
  60. )
  61. ) %>%
  62. tab_options(
  63. table.border.top.style = "hidden",
  64. table.border.bottom.style = "hidden",
  65. column_labels.border.top.style = "hidden",
  66. column_labels.border.bottom.style ="hidden"
  67. )
  68. # gtsave(filename = here::here("/2020-week37/plots/friends-table.png"))