R code for data visualization in economics, created and maintained by DIME Analytics.
# Install and load packages ---------------
packages <- c(
"tidyverse",
"haven",
"ggridges"
)
# 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")
analysis_data <- data %>%
filter(eligible == 1) %>%
mutate(group = interaction(round, treatment_locality),
group = recode_factor(group,
`0.0` = "Before & Control",
`0.1` = "Before & Treated",
`1.0` = "After & Control",
`1.1` = "After & Treated"
))
ggplot(analysis_data, aes(x = health_expenditures, y = group)) +
stat_density_ridges(alpha = 0.6, bandwidth = 0.2) +
theme_ridges() +
xlab("Health Expenditures ($)") +
ylab("Group") +
xlim(c(0, 50)) +
theme(legend.position = "none")