R code for data visualization in economics, created and maintained by DIME Analytics.
# Install and load packages ---------------
packages <- c(
"tidyverse",
"haven",
"forcats"
)
# Change to install = TRUE to install the required packages
pacman::p_load(packages, character.only = TRUE, install = FALSE)
# Load an example dataset ---------------
data <- read_dta("https://github.com/worldbank/r-econ-visual-library/raw/master/Library/Data/BarPlotsMultipleVariables.dta")
# Collapse the dataset across each treatment group
collapsed_data <- data %>%
group_by(treatment_group) %>%
summarise_at(vars(starts_with(c("bl_", "ml_"))),
list(~ mean(., na.rm = T))) %>%
ungroup()
# Reshape data
reshaped_data <- collapsed_data %>%
pivot_longer(
starts_with(c("bl_", "ml_")),
names_to = "key",
values_to = "value"
) %>%
extract(key, c("colname", "crop"),
regex = "(^bl_|^ml_)(.*)") %>%
mutate(colname = fct_rev(as.factor(paste0(colname, treatment_group)))) %>%
select(-c(treatment_group))
# Keep Summer Maize, Spring-Winter Potato, and Main Paddy
fig_data <- reshaped_data %>%
filter(
crop %in% c(
"w_Summer_Maize_Prod",
"w_Spring_Winter_Potato_Prod",
"w_Main_Paddy_Prod"
))
ggplot(fig_data, aes(x = crop, y = value, fill = colname)) +
geom_bar(
width = 0.6, position = position_dodge(width = 0.6),
stat = "identity", alpha = .8
) +
geom_text(
aes(label = format(round(value, 1), nsmall = 1)),
position = position_dodge(width = 0.6),
hjust = -0.35
) +
coord_flip(ylim = c(0, 150)) +
geom_hline(yintercept = 0, alpha = 0.5) +
scale_x_discrete(
labels = c(
"w_Summer_Maize_Prod" = "Summer Maize Prod",
"w_Spring_Winter_Potato_Prod" = "Spring Winter Potato Prod",
"w_Main_Paddy_Prod" = "Main Paddy Prod"
)) +
scale_fill_brewer(
palette = "Set2",
breaks = c("bl_0", "bl_1", "ml_0", "ml_1"),
labels = c("Baseline-Control", "Baseline-Treatment", "Midline-Control", "Midline-Treatment")
) +
ylab("Value") +
theme_classic() +
theme(
axis.line.y = element_blank(),
axis.title.y = element_blank(),
axis.title.x = element_text(size = 14),
axis.ticks.y = element_blank(),
axis.text = element_text(size = 11),
legend.title = element_blank(),
legend.text = element_text(size = 10),
legend.position = "bottom"
)