Skip to content

Instantly share code, notes, and snippets.

@lbarqueira
Last active January 5, 2024 22:31
Show Gist options
  • Save lbarqueira/c1a338a1d8d19c078a23f9014a9d9235 to your computer and use it in GitHub Desktop.
Save lbarqueira/c1a338a1d8d19c078a23f9014a9d9235 to your computer and use it in GitHub Desktop.
OECD Assault Deaths 1960-2020
## OECD Assault Deaths - LUIS BARQUEIRA
# Reference: https://kieranhealy.org/blog/archives/2023/03/30/assault-deaths-in-the-oecd-1960-2020/
# originated the gist: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx
# originated my blog post: https://lbarqueira.github.io/2023/03/29/assault-deaths-oecd.html
library(tidyverse)
library(here)
library(showtext)
## Loading Google fonts (https://fonts.google.com/)
font_add_google("Encode Sans Semi Condensed", "semiencode")
showtext_opts(dpi = 300)
## Automatically use showtext to render text
showtext_auto()
# function reusability
if (!exists("theme_definition")) {
# Check if user-defined function exists
source("ggplot2_theme.R") # Apply source function
}
theme_set(
theme_definition(base_family = "semiencode", title_family = "semiencode")
)
compare <-
c(
"AUT",
"AUS",
"BEL",
"CAN",
"DEU",
"DNK",
"ESP",
"FIN",
"FRA",
"GBR",
"GRC",
"IRL",
"ITA",
"NLD",
"NOR",
"NZL",
"SWE",
"PRT",
"USA"
)
cou_string <-
paste0(paste(compare[1:11], collapse = ", "),
",",
"\n",
paste(compare[12:17], collapse = ", "))
cou_string <- paste0(cou_string, ".")
my_colors <- c("grey50", "blue", "firebrick")
df <- read_csv(here("data", "HEALTH_STAT_05042023165047784.csv")) |>
janitor::clean_names()
names(df)
out_1 <- df |>
arrange(country, year) |>
group_by(country) |>
mutate(
us_flag = case_when(
cou == "USA" ~ "United States",
cou == "PRT" ~ "Portugal",
TRUE ~ "Seventeen other OECD Countries"
),
us_flag = factor(
us_flag,
levels = c("United States", "Portugal", "Seventeen other OECD Countries"),
ordered = TRUE
),
avg_rate = slider::slide_dbl(value, mean, .before = 3)
) |>
filter(cou %in% compare)
out <- out_1 |>
ggplot(aes(
x = year,
y = value,
group = country,
color = us_flag
)) +
geom_line(
# If I want to give different line sizes, otherwise ignore size
size = ifelse(
out_1$us_flag == "United States" | out_1$us_flag == "Portugal", .6, .5
)
) +
scale_color_manual(values = rev(my_colors)) +
labs(
color = NULL,
title = "Assault Death Rates in the OECD, 1960-2020",
x = "Year",
y = "Assault deaths per 100,000 population (standardized rates)",
caption = paste("Countries: ", cou_string, "Data: OECD. Graph: @barqueira")
)
out
ggsave(
# here::here("figures", "oecd-assault.pdf"),
here::here("figures", "oecd-assault-rev.pdf"),
out,
height = 8,
width = 10
)
ggsave(
# here::here("figures", "oecd-assault.png"),
here::here("figures", "oecd-assault-rev.png"),
out,
height = 8,
width = 10,
bg = "white"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment