/tidytuesday_202032_european_energy.r

https://github.com/spren9er/tidytuesday · R · 169 lines · 147 code · 22 blank · 0 comment · 32 complexity · 12faa7d9a363ad7c15d2393e687474ad MD5 · raw file

  1. library(tidyverse)
  2. data <- tidytuesdayR::tt_load(2020, week = 32)
  3. energy <- data$energy_types %>%
  4. mutate(
  5. type = fct_other(
  6. type,
  7. keep = c("Nuclear", "Conventional thermal"),
  8. other_level="Renewable"
  9. ),
  10. type = fct_relevel(type, "Renewable", "Nuclear", "Conventional thermal")
  11. ) %>%
  12. pivot_longer(
  13. c("2016", "2017", "2018"),
  14. names_to = "year",
  15. values_to = "total"
  16. ) %>%
  17. select(-level) %>%
  18. group_by(country_name, country, type, year) %>%
  19. summarize(total = sum(total)) %>%
  20. ungroup()
  21. countries_order <- energy %>%
  22. mutate(clean_electricity = (type == "Renewable")) %>%
  23. group_by(country, clean_electricity) %>%
  24. summarize(total = sum(total)) %>%
  25. pivot_wider(names_from = clean_electricity, values_from = total) %>%
  26. mutate(ratio = `TRUE` / (`TRUE` + `FALSE`)) %>%
  27. arrange(desc(ratio)) %>%
  28. pull(country)
  29. energy <- energy %>%
  30. mutate(
  31. country = fct_relevel(country, countries_order),
  32. year = fct_relevel(as.character(year), "2018", "2017", "2016")
  33. )
  34. color <- function(type, year) {
  35. case_when(
  36. type == "Conventional thermal" && year == 2016 ~ "#cf5c4f",
  37. type == "Conventional thermal" && year == 2017 ~ "#ca4d3f",
  38. type == "Conventional thermal" && year == 2018 ~ "#c04335",
  39. type == "Nuclear" && year == 2016 ~ "#f3e8e2",
  40. type == "Nuclear" && year == 2017 ~ "#edddd4",
  41. type == "Nuclear" && year == 2018 ~ "#e7d1c5",
  42. type == "Renewable" && year == 2016 ~ "#1f9098",
  43. type == "Renewable" && year == 2017 ~ "#1c8087",
  44. type == "Renewable" && year == 2018 ~ "#187077",
  45. TRUE ~ "#333333"
  46. )
  47. }
  48. create_marimekko_data <- function(df, ...) {
  49. columns <- enquos(...)
  50. marimekko <- df %>%
  51. mutate(overall_total = sum(total)) %>%
  52. arrange(!!!columns)
  53. for (i in seq(length(columns))) {
  54. x_total <- rlang::sym(paste0("x", i, "_total"))
  55. x_pct <- rlang::sym(paste0("x", i, "_pct"))
  56. x_total_prev <- ifelse(
  57. i == 1,
  58. rlang::sym("overall_total"),
  59. rlang::sym(paste0("x", i - 1, "_total"))
  60. )
  61. marimekko <- marimekko %>%
  62. group_by(!!!columns[1:i]) %>%
  63. mutate(
  64. !!x_total := sum(total),
  65. !!x_pct := !!x_total / !!x_total_prev
  66. )
  67. x_min_pct <- rlang::sym(paste0("x", i, "_min_pct"))
  68. x_max_pct <- rlang::sym(paste0("x", i, "_max_pct"))
  69. cum_x <- marimekko %>%
  70. group_by(!!!columns[1:i]) %>%
  71. summarize(!!x_pct := first(!!x_pct)) %>%
  72. mutate(
  73. !!x_max_pct := cumsum(!!x_pct),
  74. !!x_min_pct := lag(!!x_max_pct, default = 0)
  75. ) %>%
  76. select(-!!x_pct)
  77. marimekko <- inner_join(marimekko, cum_x)
  78. }
  79. labels <- marimekko %>%
  80. group_by(!!!columns[1], x1_max_pct, x1_min_pct) %>%
  81. summarize(label = first(!!!columns[1])) %>%
  82. ungroup() %>%
  83. transmute(ylabel = (x1_max_pct - x1_min_pct) / 2 + x1_min_pct, label)
  84. data <- marimekko %>%
  85. arrange(!!!columns) %>%
  86. ungroup() %>%
  87. rowwise() %>%
  88. mutate(
  89. xmin = x2_min_pct,
  90. xmax = x2_max_pct,
  91. ymin = x1_min_pct + (x1_max_pct - x1_min_pct) * x3_min_pct,
  92. ymax = x1_min_pct + (x1_max_pct - x1_min_pct) * x3_max_pct,
  93. color = color(type, year)
  94. ) %>%
  95. select(country, country_name, type, year, xmin, xmax, ymin, ymax, color)
  96. out <- list()
  97. out$data <- data
  98. out$labels <- labels
  99. out
  100. }
  101. marimekko <- list(
  102. create_marimekko_data(energy, year, country, type),
  103. create_marimekko_data(energy, year, type, country),
  104. create_marimekko_data(energy, type, country, year)
  105. )
  106. marimekko_data <- bind_rows(imap(marimekko, ~ mutate(.x$data, chunk_id = .y)))
  107. marimekko_data %>%
  108. filter(chunk_id == 1) %>%
  109. ggplot() +
  110. geom_rect(
  111. aes(
  112. xmin = xmin,
  113. xmax = xmax,
  114. ymin = ymin,
  115. ymax = ymax,
  116. fill = interaction(type, year),
  117. alpha = 0.1
  118. ),
  119. color = "#333333",
  120. size = 0.1,
  121. show.legend = FALSE
  122. ) +
  123. coord_fixed()
  124. marimekko_data %>%
  125. select(chunk_id, year, country, type, xmin, xmax, ymin, ymax, color) %>%
  126. arrange(chunk_id, year, country, type) %>%
  127. replace(is.na(.), 0) %>%
  128. write_csv(
  129. "data/tidytuesday_202032_european_energy.csv"
  130. )
  131. marimekko_labels <- bind_rows(
  132. imap(marimekko, ~ mutate(.x$labels, chunk_id = .y))
  133. )
  134. blank_countries <- energy %>%
  135. group_by(country) %>%
  136. summarize(total = sum(total)) %>%
  137. arrange(total) %>%
  138. head(22) %>%
  139. pull(country)
  140. marimekko_labels %>%
  141. filter(!label %in% blank_countries) %>%
  142. mutate(
  143. label = ifelse(label == "Conventional thermal", "Conv. Thermal", label)
  144. ) %>%
  145. write_csv(
  146. "data/tidytuesday_202032_european_energy_labels.csv"
  147. )