Skip to content

Instantly share code, notes, and snippets.

@USMortality
Last active April 15, 2025 13:47
Show Gist options
  • Save USMortality/ba4a16224539277dbc2d764b4f4ca8eb to your computer and use it in GitHub Desktop.
Save USMortality/ba4a16224539277dbc2d764b4f4ca8eb to your computer and use it in GitHub Desktop.
eASMR vs Government Stringency [World]
library(readr)
library(scales)
library(ggrepel)
library(ggpubr)
library(ggpmisc)
sf <- 2
width <- 600 * sf
height <- 335 * sf
options(vsc.dev.args = list(width = width, height = height, res = 72 * sf))
source(paste0(
"https://gist.githubusercontent.com/USMortality/",
"cf0dc23d0d5ca609539fe11c9c089fcc/raw/asmr_country_dataset.r"
))
df2 <- df |> filter(date < 2023, stringency_index > 0)
chart <- ggplot(df2, aes(x = stringency_index, y = asmr_who_ex_p, label = iso3c)) +
geom_text_repel() +
stat_cor(aes(label = paste(..rr.label.., ..p.label.., sep = "~`,`~")),
method = "pearson",
label.x.npc = "left",
label.y.npc = "top",
r.accuracy = 0.01,
p.accuracy = 0.01
) +
stat_poly_line() +
labs(
title = paste0(
"Oxford Government Stringency Index vs. ",
"Age Standardized Excess Mortality (2020)"
),
subtitle = paste(
"Source: Mortality.Watch, Oxford",
"Baseline: Mean 2017-'19",
sep = " · "
),
x = "Sum of Stringency Index",
y = "Excess Mortality"
) +
theme_bw() +
scale_x_continuous(labels = comma) +
scale_y_continuous(labels = scales::percent_format()) +
facet_wrap(vars(date))
ggplot2::ggsave(
filename = "chart1.png", plot = chart, width = width, height = height,
units = "px", dpi = 72 * sf, device = grDevices::png, type = c("cairo")
)
# Create the plot
# Partition the stringency column into groups
# Determine the range of stringency and break it into 5 equal intervals
df2 <- df2 |>
filter(date == "2020") |>
mutate(stringency_group = cut(stringency_index,
breaks = quantile(stringency_index,
probs = seq(0, 1, length.out = 4),
na.rm = TRUE
),
labels = c("Low", "Moderate", "High"),
include.lowest = TRUE
))
# Create the plot
chart <- ggplot(df2, aes(x = stringency_group, y = asmr_who_ex_p)) +
scale_y_continuous(labels = scales::percent_format()) +
geom_boxplot() + # Boxplot for the distribution
geom_dotplot(binaxis = "y", stackdir = "center", dotsize = 1) +
stat_summary(
fun.data = function(x) {
mean_cl_normal(x, conf.int = 0.95)
},
geom = "errorbar",
width = 0.2,
color = "red"
) +
stat_summary(
fun = mean,
geom = "point",
color = "blue",
size = 3
) +
labs(
title =
paste0(
"Age-Standardized Excess Mortality Rate (2020) ",
"by Government Stringency Group"
),
subtitle = "Median/Quartiles · Mean/95% CI",
x = "Stringency Group (by Sum of Oxford Government Stringency Index)",
y = "Age-Standardized Excess Mortality Rate (2020)"
) +
coord_cartesian(ylim = c(-0.1, .2)) +
theme_bw()
ggplot2::ggsave(
filename = "chart2.png", plot = chart, width = width, height = height,
units = "px", dpi = 72 * sf, device = grDevices::png, type = c("cairo")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment