Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

225 linhas
7.7KB

  1. ## ----setup, include=FALSE------------------------------------------------
  2. knitr::opts_chunk$set(echo=FALSE)
  3. ## ----library-------------------------------------------------------------
  4. library(dplyr)
  5. library(purrr)
  6. library(tidyr)
  7. library(stringr)
  8. library(readr)
  9. library(forcats)
  10. library(gganimate)
  11. DATA_DIR <- "data-raw"
  12. ANIM_OUT <- "anim"
  13. fs::dir_create(here::here(ANIM_OUT))
  14. pop <- readRDS(here::here("data", "pop_estimates_by_age.rds"))
  15. ## ------------------------------------------------------------------------
  16. ga <-
  17. pop %>%
  18. bind_rows() %>%
  19. mutate(age_group_int = as.integer(Age) %/% 5) %>%
  20. group_by(age_group_int) %>%
  21. mutate(
  22. # age_group = paste(min(Age), max(Age), sep = " - "),
  23. age_group = paste(min(Age)),
  24. age_group = ifelse(Age >= 100, "100+", age_group)
  25. ) %>%
  26. ungroup() %>%
  27. arrange(age_group_int, Year) %>%
  28. mutate(age_group = fct_inorder(age_group)) %>%
  29. select(-age_group_int) %>%
  30. group_by(Year, age_group) %>%
  31. summarize(Total = sum(Total)) %>%
  32. group_by(Year) %>%
  33. mutate(GrandTotal = sum(Total)) %>%
  34. ungroup() %>%
  35. mutate(Total = Total/GrandTotal) %>%
  36. arrange(Year, age_group) %>%
  37. # complete(Year, age_group, fill = list(Total = 0)) %>%
  38. # filter(Year > 2000) %>%
  39. {
  40. ggplot(.) +
  41. # aes(age_group, Total) +
  42. # geom_vline(xintercept = c(1980, 1990), color = "grey50") +
  43. # geom_line(aes(group = age_group)) +
  44. # geom_point(size = 0.2) +
  45. # geom_col(fill = "grey40") +
  46. # facet_wrap(~ Year) +
  47. # coord_flip() +
  48. # scale_y_continuous(labels = function(x) {grkmisc::pretty_num(x, decimal_digits = 0)}) +
  49. geom_segment(aes(yend = age_group, y = age_group, xend = Total), x = 0, size = 5) +
  50. geom_segment(aes(yend = age_group, y = age_group, xend = Total),
  51. x = 0,
  52. size = 5,
  53. color = "grey40",
  54. alpha = 0.25,
  55. data = filter(., Year == min(Year)) %>% select(-Year)
  56. ) +
  57. # annotate("text", label = "{closest_state}", x = max(.$Total) * 0.8, y = 20, size = 12) +
  58. theme_minimal(base_size = 16, base_family = "Fira Sans Condensed") +
  59. theme(panel.grid.major.y = element_blank()) +
  60. scale_x_continuous(labels = scales::percent, expand = c(0, 0, 0.05, 0)) +
  61. # labs(x = "Percent of Population", y = NULL) +
  62. theme(axis.title.x = element_text(hjust = 0.5, size = 30)) +
  63. labs(x = "{closest_state}", y = NULL) +
  64. gganimate::transition_time(Year) +
  65. gganimate::transition_states(Year, 1, 0, wrap = FALSE) +
  66. gganimate::ease_aes("linear") +
  67. gganimate::enter_fade()
  68. }
  69. grkmisc::logger(msg = "Creating pop bar frames")
  70. ga_files <-
  71. animate(ga,
  72. width = 2080, height = 960, res = 144, fps = 25, nframes = 161,
  73. renderer = file_renderer(here::here(ANIM_OUT, "frames"), "pop-anim", overwrite = TRUE))
  74. first_last <- first_last_new <- ga_files[c(1, length(ga_files))]
  75. first_last_new[1] <- sub("\\d+\\.png", "_first.png", first_last[1])
  76. first_last_new[2] <- sub("\\d+\\.png", "_last.png", first_last[2])
  77. fs::file_copy(first_last, fs::path(ANIM_OUT, fs::path_file(first_last_new)), overwrite = TRUE)
  78. grkmisc::logger(msg = "Writing MP4 population bar animation")
  79. av::av_encode_video(
  80. ga_files,
  81. here::here(ANIM_OUT, "pop-anim.mp4"),
  82. verbose = TRUE
  83. )
  84. grkmisc::logger(msg = "Writing gif population bar animation")
  85. gifski::gifski(
  86. ga_files,
  87. here::here(ANIM_OUT, "pop-anim.gif"),
  88. width = 2080, height = 960, delay = 1/18, loop = FALSE
  89. )
  90. unlink(ga_files)
  91. pop %>%
  92. bind_rows() %>%
  93. mutate(age_group_int = as.integer(Age) %/% 5) %>%
  94. select(1:3, age_group_int) %>%
  95. filter(age_group_int > 5) %>%
  96. mutate(
  97. old = age_group_int >= 13,
  98. old = c("Adult", "Older Adult")[old + 1]
  99. ) %>%
  100. group_by(Year, old) %>%
  101. summarize(n = sum(Total)) %>%
  102. spread(old, n) %>%
  103. {
  104. ggplot(.) +
  105. aes(x = Year) +
  106. geom_segment(aes(xend = Year, y = Adult, yend = `Older Adult`)) +
  107. geom_point(aes(y = Adult), color = grkmisc::moffitt_colors$blue) +
  108. geom_point(aes(y = `Older Adult`), color = grkmisc::moffitt_colors$orange, size = 2)
  109. } %>%
  110. invisible()
  111. elder_dependency <-
  112. pop %>%
  113. bind_rows() %>%
  114. mutate(age_group_int = as.integer(Age) %/% 5) %>%
  115. select(1:3, age_group_int) %>%
  116. mutate(
  117. old = case_when(
  118. age_group_int < 3 ~ "Children",
  119. age_group_int >= 13 ~ "Older Adult",
  120. TRUE ~ "Adult"
  121. )
  122. ) %>%
  123. group_by(Year, old) %>%
  124. summarize(n = sum(Total)) %>%
  125. spread(old, n) %>%
  126. mutate(
  127. dependency_elder = `Older Adult` / Adult,
  128. dependency_all = (`Older Adult` + Children) / Adult
  129. )
  130. ga_elder_dep <-
  131. ggplot(elder_dependency) +
  132. aes(Year, dependency_elder) +
  133. geom_point(color = "#00589a") +
  134. geom_point(data = filter(elder_dependency, Year == 2018), size = 4, color = "#eb1455") +
  135. labs(y = "Dependency Ratio", x = NULL) +
  136. theme_minimal(base_size = 24, base_family = "Fira Sans Condensed") +
  137. scale_y_continuous(labels = scales::percent) +
  138. gganimate::transition_states(Year, 1, 0, FALSE) +
  139. gganimate::shadow_mark()
  140. grkmisc::logger(msg = "Creating elder dep frames")
  141. ga_elder_dep_files <-
  142. animate(ga_elder_dep,
  143. width = 2080, height = 960, res = 144, fps = 25, nframes = 161,
  144. renderer = file_renderer(here::here(ANIM_OUT, "frames"), "pop-dependency", overwrite = TRUE))
  145. first_last <- first_last_new <- ga_elder_dep_files[c(1, length(ga_elder_dep_files))]
  146. first_last_new[1] <- sub("\\d+\\.png", "_first.png", first_last[1])
  147. first_last_new[2] <- sub("\\d+\\.png", "_last.png", first_last[2])
  148. fs::file_copy(first_last, fs::path(ANIM_OUT, fs::path_file(first_last_new)), overwrite = TRUE)
  149. grkmisc::logger(msg = "Writing mp4 elder dependency animation")
  150. av::av_encode_video(
  151. ga_elder_dep_files,
  152. output = here::here(ANIM_OUT, "pop-dependency.mp4"),
  153. verbose = TRUE
  154. )
  155. grkmisc::logger(msg = "Writing gif elder dependency animation")
  156. gifski::gifski(
  157. ga_elder_dep_files,
  158. here::here(ANIM_OUT, "pop-dependency.gif"),
  159. width = 2080, height = 960, delay = 1/25, loop = FALSE
  160. )
  161. unlink(ga_elder_dep_files)
  162. elder_dep_together <-
  163. elder_dependency %>%
  164. ungroup() %>%
  165. gather(timeseries, ratio, starts_with("depend")) %>%
  166. select(-Adult:-`Older Adult`) %>%
  167. mutate(timeseries = ifelse(timeseries == "dependency_elder", 0, 1))
  168. ga_dep_together <-
  169. ggplot(elder_dep_together) +
  170. aes(Year, ratio, group = Year) +
  171. geom_point(aes(color = paste(timeseries)), show.legend = FALSE) +
  172. scale_color_manual(values = c("#00589a", "#82c878")) +
  173. labs(y = "Dependency Ratio", x = NULL) +
  174. theme_minimal(base_size = 24, base_family = "Fira Sans Condensed") +
  175. scale_y_continuous(labels = scales::percent) +
  176. gganimate::transition_states(timeseries, 1, 0, FALSE) +
  177. gganimate::view_follow(fixed_x = TRUE) +
  178. gganimate::ease_aes("sine-in-out")+
  179. gganimate::shadow_mark()
  180. grkmisc::logger(msg = "Creating all dependency frames")
  181. ga_dep_together_files <-
  182. animate(ga_dep_together,
  183. width = 2080, height = 960, res = 144, fps = 25, nframes = 161,
  184. renderer = file_renderer(here::here(ANIM_OUT, "frames"), "pop-dependency-both", overwrite = TRUE))
  185. first_last <- first_last_new <- ga_dep_together_files[c(1, length(ga_dep_together_files))]
  186. first_last_new[1] <- sub("\\d+\\.png", "_first.png", first_last[1])
  187. first_last_new[2] <- sub("\\d+\\.png", "_last.png", first_last[2])
  188. fs::file_copy(first_last, fs::path(ANIM_OUT, fs::path_file(first_last_new)), overwrite = TRUE)
  189. grkmisc::logger(msg = "Writing mp4 all dependency animation")
  190. av::av_encode_video(
  191. ga_dep_together_files,
  192. output = here::here(ANIM_OUT, "pop-dependency-both.mp4"),
  193. verbose = TRUE
  194. )
  195. grkmisc::logger(msg = "Writing gif all dependency animation")
  196. gifski::gifski(
  197. ga_dep_together_files,
  198. here::here(ANIM_OUT, "pop-dependency-both.gif"),
  199. width = 2080, height = 960, delay = 1/25, loop = FALSE
  200. )
  201. unlink(ga_dep_together_files)