# Create DiD visualization data
did_viz <- tibble(
time = c(0, 1, 0, 1),
group = c("Control", "Control", "Treated", "Treated"),
outcome = c(2, 3, 3, 5.5),
counterfactual = c(NA, NA, NA, 4)
)
# Add counterfactual line for treated
cf_line <- tibble(
time = c(0, 1),
outcome = c(3, 4),
group = "Counterfactual"
)
ggplot() +
# Control group
geom_line(data = filter(did_viz, group == "Control"),
aes(x = time, y = outcome), color = slate_gray, linewidth = 2) +
geom_point(data = filter(did_viz, group == "Control"),
aes(x = time, y = outcome), color = slate_gray, size = 6) +
# Treated group
geom_line(data = filter(did_viz, group == "Treated"),
aes(x = time, y = outcome), color = secondary_teal, linewidth = 2) +
geom_point(data = filter(did_viz, group == "Treated"),
aes(x = time, y = outcome), color = secondary_teal, size = 6) +
# Counterfactual
geom_line(data = cf_line, aes(x = time, y = outcome),
color = accent_coral, linewidth = 1.5, linetype = "dashed") +
geom_point(data = filter(cf_line, time == 1),
aes(x = time, y = outcome), color = accent_coral, size = 5, shape = 1, stroke = 2) +
# Treatment effect arrow
annotate("segment", x = 1.05, y = 4, xend = 1.05, yend = 5.5,
arrow = arrow(length = unit(0.3, "cm"), ends = "both"),
color = warm_gold, linewidth = 1.5) +
annotate("text", x = 1.15, y = 4.75, label = "Treatment\nEffect",
color = warm_gold, fontface = "bold", size = 5, hjust = 0) +
# Labels
annotate("text", x = -0.05, y = 2, label = "Control (Pre)", hjust = 1, color = slate_gray, size = 4) +
annotate("text", x = 1.05, y = 3, label = "Control (Post)", hjust = 0, color = slate_gray, size = 4) +
annotate("text", x = -0.05, y = 3, label = "Treated (Pre)", hjust = 1, color = secondary_teal, size = 4) +
annotate("text", x = 1.05, y = 5.5, label = "Treated (Post)", hjust = 0, color = secondary_teal, size = 4) +
annotate("text", x = 1.05, y = 4, label = "Counterfactual", hjust = 0, color = accent_coral, size = 4) +
# Vertical line at treatment
geom_vline(xintercept = 0.5, linetype = "dotted", color = slate_gray, linewidth = 1) +
annotate("text", x = 0.5, y = 6.2, label = "Treatment\nOccurs", color = slate_gray, size = 4) +
scale_x_continuous(breaks = c(0, 1), labels = c("Pre", "Post"), limits = c(-0.2, 1.5)) +
scale_y_continuous(limits = c(1.5, 6.5)) +
labs(
title = "The DiD Estimator: Parallel Trends in Action",
subtitle = "The control group trend estimates what would have happened to treated units",
x = "", y = "Outcome"
) +
theme_health_econ(base_size = 18) +
theme(panel.grid = element_blank())