R code for data visualization in economics, created and maintained by DIME Analytics.
# Install and load packages ---------------
packages <- c(
"tidyverse",
"haven",
"rdd",
"splines"
)
# Change to install = TRUE to install the required packages
pacman::p_load(packages, character.only = TRUE, install = FALSE)
# Load an example dataset ---------------
# https://openknowledge.worldbank.org/handle/10986/25030
data <- read_dta("https://github.com/worldbank/r-econ-visual-library/raw/master/Library/Data/evaluation.dta")
cutoff <- 58
data <- data %>%
filter(treatment_locality == 1) %>%
mutate(treatment = (poverty_index <= cutoff))
ggplot(data, aes(x = poverty_index, y = health_expenditures, color = treatment)) +
geom_point(alpha = 0.2, size = 0.1) +
geom_smooth(method = lm, formula = y ~ bs(x, 3), size = 1.0, se = FALSE) +
geom_vline(xintercept = cutoff, linetype = "longdash") +
ylim(c(0, 45)) +
xlab("Baseline Poverty Index") +
ylab("Health Expenditures ($)") +
scale_color_brewer(palette = "Set2", labels = c("Not eligible", "Eligible")) +
theme_classic() +
theme(
axis.text = element_text(size = 12),
axis.title = element_text(size = 14),
legend.title = element_blank(),
legend.text = element_text(size = 11)
)