Skip to content

Instantly share code, notes, and snippets.

@expersso
Last active June 15, 2016 11:52
Show Gist options
  • Save expersso/fb251508dc71a6c4a3723f5e4c8a0ff9 to your computer and use it in GitHub Desktop.
Save expersso/fb251508dc71a6c4a3723f5e4c8a0ff9 to your computer and use it in GitHub Desktop.
Remake of World Bank chart
# Remake of http://blogs.worldbank.org/opendata/measuring-surgical-systems-new-paradigm-health-systems-strengthening
library(dplyr)
library(purrr)
library(ggplot2)
library(scales)
library(ggthemes)
library(readxl)
library(tidyr)
library(wbstats)
library(countrycode)
library(ggrepel)
get_surgery_data <- function() {
surgery_url <- paste0("http://media.wix.com/ugd/346076_c5fd14c703784249aae",
"b8d9a43df34bc.xlsx?dn=Appendix2_Ind2_WDI_2015.xlsx")
tmp <- tempfile(fileext = ".xlsx")
download.file(surgery_url, tmp, mode = "wb")
surgery <- read_excel(tmp, skip = 2)
names(surgery)[is.na(names(surgery))] <- c("country", "income_lvl", "source")
surgery
}
surgery <- get_surgery_data() %>%
gather(date, value, -country, -income_lvl, -source) %>%
mutate(iso2c = countrycode(country, "country.name", "iso2c")) %>%
filter(!is.na(value)) %>%
arrange(country, date) %>%
group_by(country) %>%
slice(1) %>%
ungroup() %>%
select(iso2c, "surgery" = value)
life_exp <- wb("all", "SP.DYN.LE00.IN", mrv = 1) %>%
tbl_df() %>%
select(iso2c, "life_exp" = value)
pop <- wb("all", "SP.POP.TOTL", mrv = 1) %>%
tbl_df() %>%
select(iso2c, "pop" = value)
df <- reduce(list(life_exp, pop, surgery, wbcountries()), inner_join)
df$region <- gsub("\\(.*\\)", "", df$region)
df$residual <- loess(life_exp ~ surgery, df)$residuals %>% abs()
ggplot(df, aes(x = surgery, y = life_exp, size = pop, color = region)) +
geom_vline(xintercept = c(20, 40), color = "grey60") +
geom_point(alpha = 0.25) +
geom_smooth(color = "whitesmoke", alpha = 0.2) +
geom_text_repel(aes(label = country), top_n(df, 25, residual), size = 3,
show.legend = FALSE) +
scale_size_area(breaks = pretty_breaks(4), max_size = 10) +
scale_x_log10(breaks = c(1, 20, 40, 100)) +
scale_color_few("dark") +
theme_light() +
theme(text = element_text(color = "grey40"),
axis.title = element_text(size = rel(0.8)),
legend.key = element_blank(),
panel.background = element_rect(fill = "grey99"),
plot.caption = element_text(size = rel(0.6),
margin = unit(c(5, 5, 0, 5), "mm"))) +
guides(size = "none", color = guide_legend(override.aes = list(size = 5))) +
labs(x = "Specialist surgical workers (per 100,000 people)",
y = "Life expectancy at birth (years)\n",
color = NULL,
title = paste0("Life expectancy vs. the relative number of ",
"surgical workers in a country"),
subtitle = "Estimates around 2013",
caption = paste0("Source: World Bank, Lancet Commission\nNote: ",
"Vertical lines indicate Lancet Commission benchmarks")
)
@expersso
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment