Advertisement
Guest User

Place of death by age

a guest
Jun 24th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 KB | None | 0 0
  1. library(conflicted)
  2. library(tidyverse)
  3. library(scales)
  4. filter <- dplyr::filter
  5.  
  6. # partial() is an awesome function from purrr
  7. full_percent <- partial(percent, accuracy = 1)
  8.  
  9. # After gathering my data, I used dput() to create the reproducible version here
  10. death <- structure(list(age = c("< 1 year", "< 1 year", "< 1 year", "< 1 year",
  11. "< 1 year", "< 1 year", "< 1 year", "< 1 year", "1-4 years",
  12. "1-4 years", "1-4 years", "1-4 years", "1-4 years", "1-4 years",
  13. "1-4 years", "1-4 years", "5-14 years", "5-14 years", "5-14 years",
  14. "5-14 years", "5-14 years", "5-14 years", "5-14 years", "5-14 years",
  15. "15-24 years", "15-24 years", "15-24 years", "15-24 years", "15-24 years",
  16. "15-24 years", "15-24 years", "15-24 years", "25-34 years", "25-34 years",
  17. "25-34 years", "25-34 years", "25-34 years", "25-34 years", "25-34 years",
  18. "25-34 years", "35-44 years", "35-44 years", "35-44 years", "35-44 years",
  19. "35-44 years", "35-44 years", "35-44 years", "35-44 years", "45-54 years",
  20. "45-54 years", "45-54 years", "45-54 years", "45-54 years", "45-54 years",
  21. "45-54 years", "45-54 years", "55-64 years", "55-64 years", "55-64 years",
  22. "55-64 years", "55-64 years", "55-64 years", "55-64 years", "55-64 years",
  23. "65-74 years", "65-74 years", "65-74 years", "65-74 years", "65-74 years",
  24. "65-74 years", "65-74 years", "65-74 years", "75-84 years", "75-84 years",
  25. "75-84 years", "75-84 years", "75-84 years", "75-84 years", "75-84 years",
  26. "75-84 years", "85+ years", "85+ years", "85+ years", "85+ years",
  27. "85+ years", "85+ years", "85+ years", "85+ years"), place_of_death = c("Hospital",
  28. "Hospital", "Other/Unknown/DOA",
  29. "At home", "Long term care/Hospice", "Long term care/Hospice",
  30. "Other/Unknown/DOA", "Other/Unknown/DOA", "Hospital",
  31. "Hospital", "Other/Unknown/DOA",
  32. "At home", "Long term care/Hospice", "Long term care/Hospice",
  33. "Other/Unknown/DOA", "Other/Unknown/DOA", "Hospital",
  34. "Hospital", "Other/Unknown/DOA",
  35. "At home", "Long term care/Hospice", "Long term care/Hospice",
  36. "Other/Unknown/DOA", "Other/Unknown/DOA", "Hospital",
  37. "Hospital", "Other/Unknown/DOA",
  38. "At home", "Long term care/Hospice", "Long term care/Hospice",
  39. "Other/Unknown/DOA", "Other/Unknown/DOA", "Hospital",
  40. "Hospital", "Other/Unknown/DOA",
  41. "At home", "Long term care/Hospice", "Long term care/Hospice",
  42. "Other/Unknown/DOA", "Other/Unknown/DOA", "Hospital",
  43. "Hospital", "Other/Unknown/DOA",
  44. "At home", "Long term care/Hospice", "Long term care/Hospice",
  45. "Other/Unknown/DOA", "Other/Unknown/DOA", "Hospital",
  46. "Hospital", "Other/Unknown/DOA",
  47. "At home", "Long term care/Hospice", "Long term care/Hospice",
  48. "Other/Unknown/DOA", "Other/Unknown/DOA", "Hospital",
  49. "Hospital", "Other/Unknown/DOA",
  50. "At home", "Long term care/Hospice", "Long term care/Hospice",
  51. "Other/Unknown/DOA", "Other/Unknown/DOA", "Hospital",
  52. "Hospital", "Other/Unknown/DOA",
  53. "At home", "Long term care/Hospice", "Long term care/Hospice",
  54. "Other/Unknown/DOA", "Other/Unknown/DOA", "Hospital",
  55. "Hospital", "Other/Unknown/DOA",
  56. "At home", "Long term care/Hospice", "Long term care/Hospice",
  57. "Other/Unknown/DOA", "Other/Unknown/DOA", "Hospital",
  58. "Hospital", "Other/Unknown/DOA",
  59. "At home", "Long term care/Hospice", "Long term care/Hospice",
  60. "Other/Unknown/DOA", "Other/Unknown/DOA"), deaths = c(184882L, 31405L,
  61. 3090L, 17360L, 565L, 222L, 3358L, 1395L, 16450L, 12858L, 1031L,
  62. 7939L, 239L, 126L, 2891L, 214L, 19935L, 12190L, 1112L, 12912L,
  63. 442L, 247L, 7145L, 274L, 65727L, 56277L, 7668L, 65747L, 1963L,
  64. 1387L, 102543L, 2073L, 113022L, 78149L, 9829L, 138663L, 6884L,
  65. 4802L, 122703L, 3311L, 218072L, 104785L, 11866L, 234876L, 22294L,
  66. 17077L, 117003L, 5395L, 600588L, 205257L, 20476L, 617612L, 84709L,
  67. 80438L, 170368L, 12372L, 1199201L, 304671L, 25679L, 1158443L,
  68. 199847L, 244489L, 194821L, 19232L, 1663359L, 323797L, 22704L,
  69. 1449572L, 309373L, 543455L, 186189L, 23387L, 2093188L, 330660L,
  70. 20014L, 1773902L, 435922L, 1380850L, 269879L, 30934L, 1943991L,
  71. 266752L, 15997L, 1897304L, 505378L, 2977469L, 444827L, 31099L
  72. )), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA,
  73. -88L), spec = structure(list(cols = list(age = structure(list(), class = c("collector_character",
  74. "collector")), place_of_death = structure(list(), class = c("collector_character",
  75. "collector")), deaths = structure(list(), class = c("collector_integer",
  76. "collector"))), default = structure(list(), class = c("collector_guess",
  77. "collector")), skip = 1), class = "col_spec"))
  78.  
  79. death <- death %>%
  80. filter(age != "< 1 year") %>%
  81. group_by(age, place_of_death) %>%
  82. summarise(deaths = sum(deaths)) %>%
  83. ungroup() %>%
  84. group_by(age) %>%
  85. mutate(total_deaths_per_age = sum(deaths)) %>%
  86. ungroup() %>%
  87. mutate(percent_deaths = deaths / total_deaths_per_age) %>%
  88. mutate(age = ordered(
  89. age,
  90. levels = c(
  91. "1-4 years",
  92. "5-14 years",
  93. "15-24 years",
  94. "25-34 years",
  95. "35-44 years",
  96. "45-54 years",
  97. "55-64 years",
  98. "65-74 years",
  99. "75-84 years",
  100. "85+ years"
  101. )
  102. )) %>%
  103. arrange(age) %>%
  104. select(age, place_of_death, percent_deaths)
  105.  
  106. ggplot(death,
  107. aes(
  108. x = age,
  109. y = percent_deaths,
  110. color = place_of_death,
  111. group = place_of_death
  112. )) +
  113. geom_point() +
  114. geom_line(size = 2) +
  115. scale_color_manual(values = c("#33a02c", "#1f78b4", "#984ea3", "orange")) +
  116. scale_y_continuous(labels = full_percent) +
  117. theme(
  118. panel.grid.major = element_line(colour = "gray90"),
  119. panel.grid.minor = element_line(colour = NA),
  120. plot.title = element_text(size = 28,
  121. hjust = 0.0),
  122. legend.title = element_text(size = 11,
  123. face = "bold"),
  124. panel.background = element_rect(fill = NA),
  125. legend.key = element_rect(fill = NA),
  126. legend.background = element_rect(fill = NA),
  127. legend.position = "top",
  128. legend.direction = "horizontal"
  129. ) + labs(
  130. title = "Where do people die?",
  131. x = "Age group",
  132. y = "Percent of US deaths occuring in given location",
  133. colour = "",
  134. subtitle = "According to the Centers for Disease Control and Prevention (CDC),\nmost people over the age of 14 die outside of the hospital",
  135. caption = "\nCreated by TrueBirch using data from wonder.cdc.gov. Based on 25,686,326 deaths in the United States from 2008-2017."
  136. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement