Last active
December 14, 2015 10:34
-
-
Save expersso/25acc9fdd896a4b601bc to your computer and use it in GitHub Desktop.
Plot of assault deaths by country using the OECD package
This file contains 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(OECD) | |
# Retrieve data from OECD for all countries | |
df <- get_dataset("HEALTH_STAT", list("CICDHOCD.TXCMFETF+TXCMHOTH+TXCMILTX")) %>% | |
tbl_df() %>% | |
setNames(tolower(names(.))) | |
# Get list of dataframes with human-readable descriptions of variables and units | |
dims <- get_data_structure("HEALTH_STAT") | |
# Add descriptions | |
df <- left_join(df, dims[["VAR"]], by = c("var" = "id")) | |
df <- left_join(df, dims[["UNIT"]], by = c("unit" = "id")) | |
df <- left_join(df, dims[["COU"]], by = c("cou" = "id")) | |
names(df)[8:10] <- c("death_reason", "unit_desc", "country_desc") | |
# Get mean deaths by country and group (1990-2015) | |
df_sum <- df %>% | |
filter(obstime >= 1990) %>% | |
group_by(country_desc, unit_desc) %>% | |
summarise(value = mean(obsvalue)) %>% | |
mutate(unit_desc = str_trim(str_replace(unit_desc, "\\(.*\\)", ""))) %>% | |
ungroup() | |
# Get order of countries, for ordering in chart | |
country_lvls <- df_sum %>% | |
filter(unit_desc == "Deaths per 100 000 population") %>% | |
arrange(value) %>% | |
.$country_desc | |
df_sum %>% | |
spread(unit_desc, value) %>% | |
ggplot(aes(y = factor(country_desc, country_lvls))) + | |
geom_segment(aes(x = `Deaths per 100 000 females`, | |
xend = `Deaths per 100 000 males`, | |
yend = factor(country_desc, country_lvls)), | |
color = "white", size = 3) + | |
geom_text(aes(label = country_desc, x = `Deaths per 100 000 population`), | |
size = 3, nudge_y = 0.1) + | |
geom_point(aes(x = value, color = unit_desc, shape = unit_desc), size = 4, | |
data = filter(df_sum, unit_desc != "Deaths per 100 000 population")) + | |
geom_point(aes(x = value, color = unit_desc, shape = unit_desc), size = 2, | |
position = position_nudge(y = -0.5), | |
data = filter(df_sum, unit_desc == "Deaths per 100 000 population")) + | |
scale_x_log10() + | |
scale_shape_manual(values = c(124, 124, 17), | |
labels = c("Females", "Males", "Total population")) + | |
scale_color_manual(values = c("red", "blue", "grey50"), | |
labels = c("Females", "Males", "Total population")) + | |
theme(axis.text.y = element_blank(), | |
axis.ticks.y = element_blank(), | |
panel.grid = element_blank(), | |
panel.background = element_rect(fill = "grey95"), | |
legend.position = "bottom") + | |
labs(y = NULL, color = NULL, shape = NULL, | |
x = "Deaths per 100,00 (log scale)", | |
title = "Mean annual deaths from assault (1990-2015)") |
Author
expersso
commented
Dec 14, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment