/tidytuesday_201945_bike_and_walk_commutes.r

https://github.com/spren9er/tidytuesday · R · 93 lines · 86 code · 7 blank · 0 comment · 1 complexity · ebb148e2d0eb6918295ef05f157c8256 MD5 · raw file

  1. library(tidyverse)
  2. library(geofacet)
  3. library(xkcd)
  4. path <-
  5. paste0(
  6. 'https://raw.githubusercontent.com/rfordatascience/tidytuesday/',
  7. 'master/data/2019/2019-11-05/'
  8. )
  9. data <- read_csv(paste0(path, 'commute.csv'))
  10. totals <- data %>%
  11. mutate(
  12. state_abb = ifelse(state == 'District of Columbia', 'DC', state_abb)
  13. ) %>%
  14. drop_na(state_abb) %>%
  15. group_by(mode, state, state_abb, city_size) %>%
  16. summarize(total = sum(n)) %>%
  17. ungroup() %>%
  18. mutate(
  19. mode = str_to_lower(mode),
  20. city_size = str_to_lower(city_size)
  21. )
  22. percentages <- totals %>%
  23. group_by(state, state_abb, city_size) %>%
  24. mutate(total_city_size = sum(total)) %>%
  25. group_by(state, state_abb) %>%
  26. mutate(
  27. total_state = sum(total),
  28. percentage_city_size = total_city_size / total_state,
  29. percentage = total / total_city_size
  30. ) %>%
  31. pivot_wider(names_from = mode, values_from = c(percentage, total)) %>%
  32. select(
  33. state, state_abb, city_size, percentage_city_size, percentage_walk,
  34. percentage_bike
  35. ) %>%
  36. arrange(state, state_abb, city_size)
  37. cum_percentages <- percentages %>%
  38. group_by(state, state_abb) %>%
  39. mutate(
  40. cum_percentage_city_size = cumsum(percentage_city_size),
  41. lag_cum_percentage_city_size = lag(cum_percentage_city_size, default = 0)
  42. )
  43. cum_percentages %>%
  44. ggplot() +
  45. xkcdrect(
  46. data = cum_percentages,
  47. aes(
  48. xmin = 0, xmax = percentage_walk,
  49. ymin = lag_cum_percentage_city_size, ymax = cum_percentage_city_size,
  50. fill = paste('walk', city_size, sep = ' / ')
  51. ), size = 0.1, show.legend = FALSE) +
  52. xkcdrect(
  53. data = cum_percentages,
  54. aes(
  55. xmin = percentage_walk, xmax = 1,
  56. ymin = lag_cum_percentage_city_size, ymax = cum_percentage_city_size,
  57. fill = paste('bike', city_size, sep = ' / ')
  58. ), size = 0.1, show.legend = FALSE) +
  59. scale_fill_manual(
  60. values = c(
  61. '#161A29', '#11433F', '#217D66', '#7E4997', '#726BA4', '#85A6CD'
  62. )
  63. ) +
  64. coord_fixed() +
  65. facet_geo(~state_abb) +
  66. theme_void() +
  67. theme(
  68. text = element_text(family = 'xkcd Script'),
  69. strip.background = element_blank(),
  70. plot.title = element_text(
  71. margin = margin(t = 15), size = 15, hjust = 0.5
  72. ),
  73. plot.subtitle = element_text(
  74. margin = margin(t = 10, b = 11), size = 11, hjust = 0.5
  75. ),
  76. plot.caption = element_text(
  77. color = '#dedede', size = 7, margin = margin(t = 5, b = 6),
  78. hjust = 0.995
  79. )
  80. ) +
  81. labs(
  82. title = 'Walk/Bike Ratios of Commutes in U.S. 2008 - 2012',
  83. subtitle = '(per State & City Size) #tidytuesday 45/2019',
  84. caption = '© 2019 spren9er'
  85. )
  86. ggsave('images/tidytuesday_201945_bike_and_walk_commutes.png', dpi = 150)