Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(conflicted)
- library(tidyverse)
- library(scales)
- filter <- dplyr::filter
- # partial() is an awesome function from purrr
- full_percent <- partial(percent, accuracy = 1)
- # After gathering my data, I used dput() to create the reproducible version here
- death <- structure(list(age = c("< 1 year", "< 1 year", "< 1 year", "< 1 year",
- "< 1 year", "< 1 year", "< 1 year", "< 1 year", "1-4 years",
- "1-4 years", "1-4 years", "1-4 years", "1-4 years", "1-4 years",
- "1-4 years", "1-4 years", "5-14 years", "5-14 years", "5-14 years",
- "5-14 years", "5-14 years", "5-14 years", "5-14 years", "5-14 years",
- "15-24 years", "15-24 years", "15-24 years", "15-24 years", "15-24 years",
- "15-24 years", "15-24 years", "15-24 years", "25-34 years", "25-34 years",
- "25-34 years", "25-34 years", "25-34 years", "25-34 years", "25-34 years",
- "25-34 years", "35-44 years", "35-44 years", "35-44 years", "35-44 years",
- "35-44 years", "35-44 years", "35-44 years", "35-44 years", "45-54 years",
- "45-54 years", "45-54 years", "45-54 years", "45-54 years", "45-54 years",
- "45-54 years", "45-54 years", "55-64 years", "55-64 years", "55-64 years",
- "55-64 years", "55-64 years", "55-64 years", "55-64 years", "55-64 years",
- "65-74 years", "65-74 years", "65-74 years", "65-74 years", "65-74 years",
- "65-74 years", "65-74 years", "65-74 years", "75-84 years", "75-84 years",
- "75-84 years", "75-84 years", "75-84 years", "75-84 years", "75-84 years",
- "75-84 years", "85+ years", "85+ years", "85+ years", "85+ years",
- "85+ years", "85+ years", "85+ years", "85+ years"), place_of_death = c("Hospital",
- "Hospital", "Other/Unknown/DOA",
- "At home", "Long term care/Hospice", "Long term care/Hospice",
- "Other/Unknown/DOA", "Other/Unknown/DOA", "Hospital",
- "Hospital", "Other/Unknown/DOA",
- "At home", "Long term care/Hospice", "Long term care/Hospice",
- "Other/Unknown/DOA", "Other/Unknown/DOA", "Hospital",
- "Hospital", "Other/Unknown/DOA",
- "At home", "Long term care/Hospice", "Long term care/Hospice",
- "Other/Unknown/DOA", "Other/Unknown/DOA", "Hospital",
- "Hospital", "Other/Unknown/DOA",
- "At home", "Long term care/Hospice", "Long term care/Hospice",
- "Other/Unknown/DOA", "Other/Unknown/DOA", "Hospital",
- "Hospital", "Other/Unknown/DOA",
- "At home", "Long term care/Hospice", "Long term care/Hospice",
- "Other/Unknown/DOA", "Other/Unknown/DOA", "Hospital",
- "Hospital", "Other/Unknown/DOA",
- "At home", "Long term care/Hospice", "Long term care/Hospice",
- "Other/Unknown/DOA", "Other/Unknown/DOA", "Hospital",
- "Hospital", "Other/Unknown/DOA",
- "At home", "Long term care/Hospice", "Long term care/Hospice",
- "Other/Unknown/DOA", "Other/Unknown/DOA", "Hospital",
- "Hospital", "Other/Unknown/DOA",
- "At home", "Long term care/Hospice", "Long term care/Hospice",
- "Other/Unknown/DOA", "Other/Unknown/DOA", "Hospital",
- "Hospital", "Other/Unknown/DOA",
- "At home", "Long term care/Hospice", "Long term care/Hospice",
- "Other/Unknown/DOA", "Other/Unknown/DOA", "Hospital",
- "Hospital", "Other/Unknown/DOA",
- "At home", "Long term care/Hospice", "Long term care/Hospice",
- "Other/Unknown/DOA", "Other/Unknown/DOA", "Hospital",
- "Hospital", "Other/Unknown/DOA",
- "At home", "Long term care/Hospice", "Long term care/Hospice",
- "Other/Unknown/DOA", "Other/Unknown/DOA"), deaths = c(184882L, 31405L,
- 3090L, 17360L, 565L, 222L, 3358L, 1395L, 16450L, 12858L, 1031L,
- 7939L, 239L, 126L, 2891L, 214L, 19935L, 12190L, 1112L, 12912L,
- 442L, 247L, 7145L, 274L, 65727L, 56277L, 7668L, 65747L, 1963L,
- 1387L, 102543L, 2073L, 113022L, 78149L, 9829L, 138663L, 6884L,
- 4802L, 122703L, 3311L, 218072L, 104785L, 11866L, 234876L, 22294L,
- 17077L, 117003L, 5395L, 600588L, 205257L, 20476L, 617612L, 84709L,
- 80438L, 170368L, 12372L, 1199201L, 304671L, 25679L, 1158443L,
- 199847L, 244489L, 194821L, 19232L, 1663359L, 323797L, 22704L,
- 1449572L, 309373L, 543455L, 186189L, 23387L, 2093188L, 330660L,
- 20014L, 1773902L, 435922L, 1380850L, 269879L, 30934L, 1943991L,
- 266752L, 15997L, 1897304L, 505378L, 2977469L, 444827L, 31099L
- )), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA,
- -88L), spec = structure(list(cols = list(age = structure(list(), class = c("collector_character",
- "collector")), place_of_death = structure(list(), class = c("collector_character",
- "collector")), deaths = structure(list(), class = c("collector_integer",
- "collector"))), default = structure(list(), class = c("collector_guess",
- "collector")), skip = 1), class = "col_spec"))
- death <- death %>%
- filter(age != "< 1 year") %>%
- group_by(age, place_of_death) %>%
- summarise(deaths = sum(deaths)) %>%
- ungroup() %>%
- group_by(age) %>%
- mutate(total_deaths_per_age = sum(deaths)) %>%
- ungroup() %>%
- mutate(percent_deaths = deaths / total_deaths_per_age) %>%
- mutate(age = ordered(
- age,
- levels = c(
- "1-4 years",
- "5-14 years",
- "15-24 years",
- "25-34 years",
- "35-44 years",
- "45-54 years",
- "55-64 years",
- "65-74 years",
- "75-84 years",
- "85+ years"
- )
- )) %>%
- arrange(age) %>%
- select(age, place_of_death, percent_deaths)
- ggplot(death,
- aes(
- x = age,
- y = percent_deaths,
- color = place_of_death,
- group = place_of_death
- )) +
- geom_point() +
- geom_line(size = 2) +
- scale_color_manual(values = c("#33a02c", "#1f78b4", "#984ea3", "orange")) +
- scale_y_continuous(labels = full_percent) +
- theme(
- panel.grid.major = element_line(colour = "gray90"),
- panel.grid.minor = element_line(colour = NA),
- plot.title = element_text(size = 28,
- hjust = 0.0),
- legend.title = element_text(size = 11,
- face = "bold"),
- panel.background = element_rect(fill = NA),
- legend.key = element_rect(fill = NA),
- legend.background = element_rect(fill = NA),
- legend.position = "top",
- legend.direction = "horizontal"
- ) + labs(
- title = "Where do people die?",
- x = "Age group",
- y = "Percent of US deaths occuring in given location",
- colour = "",
- subtitle = "According to the Centers for Disease Control and Prevention (CDC),\nmost people over the age of 14 die outside of the hospital",
- caption = "\nCreated by TrueBirch using data from wonder.cdc.gov. Based on 25,686,326 deaths in the United States from 2008-2017."
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement