Created
June 14, 2025 16:41
-
-
Save USMortality/1520295418b90591eeb3e61fae5cd3ae to your computer and use it in GitHub Desktop.
Total Deaths/Mortality by Selected UCD Chapters [USA, Western Census Region]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(data.table) | |
library(ggplot2) | |
sf <- 2 | |
width <- 900 * sf | |
height <- 450 * sf | |
options(vsc.dev.args = list(width = width, height = height, res = 72 * sf)) | |
# Load data | |
s <- fread( | |
"https://s3.mortality.watch/data/population/usa/usa_states_iso3c.csv" | |
) | |
p <- fread("https://s3.mortality.watch/data/population/usa/20y.csv")[ | |
age_group == "all" | |
] | |
d1 <- fread( | |
"https://apify.mortality.watch/cdc-wonder/year-icd_chapter.txt", | |
select = c("State", "Year Code", "UCD - ICD Chapter Code", "Deaths") | |
) | |
setnames(d1, c("jurisdiction", "year", "ucd-chapter", "deaths")) | |
d2 <- fread( | |
"https://apify.mortality.watch/cdc-wonder/provisional-year-icd_chapter.txt", | |
select = c("Residence State", "Year Code", "UCD - ICD Chapter Code", "Deaths") | |
) | |
setnames(d2, c("jurisdiction", "year", "ucd-chapter", "deaths")) | |
df <- p[d2[s, on = .(jurisdiction)], on = .(iso3c, year), nomatch = NULL] | |
df <- df[year < 2025] | |
western_states <- c( | |
# Mountain Division | |
"Arizona", "Colorado", "Idaho", "Montana", | |
"Nevada", "New Mexico", "Utah", "Wyoming", | |
# Pacific Division | |
"Alaska", "California", "Hawaii", "Oregon", "Washington" | |
) | |
# Handle suppressed values and convert to numeric | |
df[deaths == "Suppressed", deaths := "5"] | |
df[, deaths := as.numeric(deaths)] | |
# Filter for the specified ICD chapters and Western states | |
comparison_data <- df[jurisdiction %in% western_states & | |
`ucd-chapter` %in% c("U00-U99", "C00-D48", "I00-I99", "J00-J98")] | |
# Calculate mortality rates and deaths - U00-U99 vs sum of the other three | |
mortality_comparison <- comparison_data[, .( | |
total_deaths = sum(as.numeric(deaths), na.rm = TRUE), | |
total_population = sum(population, na.rm = TRUE) | |
), by = .(year, `ucd-chapter`)][ | |
, | |
mortality_rate := (total_deaths / total_population) * 100000 | |
] | |
# Create the comparison for both mortality rates and deaths | |
final_comparison <- rbind( | |
# U00-U99 rates and deaths | |
mortality_comparison[`ucd-chapter` == "U00-U99", .( | |
year, | |
category = "U00-U99", | |
mortality_rate, | |
deaths = total_deaths | |
)], | |
# Sum of the other three categories - CORRECTED | |
mortality_comparison[`ucd-chapter` %in% c("C00-D48", "I00-I99", "J00-J98"), .( | |
total_deaths = sum(total_deaths, na.rm = TRUE), | |
total_population = sum(total_population, na.rm = TRUE) | |
), by = year][, .( | |
year, | |
category = "C00-D48 + I00-I99 + J00-J98", | |
mortality_rate = (total_deaths / total_population) * 100000, | |
deaths = total_deaths | |
)] | |
) | |
# Create mortality rate chart | |
chart_rates <- ggplot(final_comparison, aes(x = year, y = mortality_rate)) + | |
geom_line(linewidth = 1) + | |
geom_point() + | |
labs( | |
title = "Crude Mortality Rate by Selected UCD Chapters [USA, Western Census Region]", | |
subtitle = "Source: CDC Wonder, U00-U99 vs Combined C00-D48 + I00-I99 + J00-J98", | |
x = "Year", | |
y = "Deaths per 100,000 population" | |
) + | |
facet_wrap(~category, scales = "free") + | |
theme_minimal() | |
# Create deaths chart | |
chart_deaths <- ggplot(final_comparison, aes(x = year, y = deaths)) + | |
geom_line(linewidth = 1) + | |
geom_point() + | |
labs( | |
title = "Total Deaths by Selected UCD Chapters [USA, Western Census Region]", | |
subtitle = "Source: CDC Wonder, U00-U99 vs Combined C00-D48 + I00-I99 + J00-J98", | |
x = "Year", | |
y = "Total Deaths" | |
) + | |
facet_wrap(~category, scales = "free") + | |
theme_minimal() | |
# Combine both charts into a 2x2 grid | |
library(gridExtra) | |
combined_chart <- grid.arrange(chart_deaths, chart_rates, ncol = 1) | |
ggsave( | |
filename = "chart1.png", | |
plot = combined_chart, | |
width = width, | |
height = height * 2, # Double height for 2 charts | |
units = "px", | |
dpi = 72 * sf, | |
device = grDevices::png, | |
type = "cairo" | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment