# 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 trend for treated units (parallel to control)
cf_line <- tibble(
time = c(0, 0.5, 1),
outcome = c(3, 3.5, 4),
group = "Counterfactual"
)
# Observed treated path:
# follows counterfactual until intervention, then kinks to a new slope
treated_path <- tibble(
time = c(0, 0.5, 1),
outcome = c(3, 3.5, 5.5)
)
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) +
# Counterfactual
geom_line(data = cf_line, aes(x = time, y = outcome),
color = accent_coral, linewidth = 1.5, linetype = "dashed") +
# Treated group (piecewise with post-treatment kink)
geom_line(data = treated_path,
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) +
geom_point(data = filter(treated_path, time == 0.5),
aes(x = time, y = outcome), color = secondary_teal, size = 4) +
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.08, y = 4, xend = 1.08, yend = 5.5,
arrow = arrow(length = unit(0.3, "cm"), ends = "both"),
color = warm_gold, linewidth = 1.5) +
annotate("text", x = 1.22, y = 4.75, label = "Treatment\nEffect",
color = warm_gold, fontface = "bold", size = 4.5, hjust = 0) +
# Labels
annotate("text", x = -0.05, y = 1.8, label = "Control (Pre)", hjust = 1, color = slate_gray, size = 3.8) +
annotate("text", x = 1.08, y = 2.7, label = "Control (Post)", hjust = 0, color = slate_gray, size = 3.8) +
annotate("text", x = -0.05, y = 3.2, label = "Treated (Pre)", hjust = 1, color = secondary_teal, size = 3.8) +
annotate("text", x = 1.08, y = 5.8, label = "Treated (Post)", hjust = 0, color = secondary_teal, size = 3.8) +
annotate("text", x = 1.08, y = 3.7, label = "Counterfactual", hjust = 0, color = accent_coral, size = 3.8) +
# 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())