# Treated and control groups, pre/post policy
df_did <- tibble(
time = rep(c("Pre","Post"), each=2),
group = rep(c("Treated","Control"), 2),
mean = c(55, 50, 45, 48) # treated drops 10, control drops 2
) |> mutate(
time = factor(time, levels=c("Pre","Post")),
counterfactual = c(NA, NA, NA, 55 - (50-48)) # what treated would have been
)
ggplot(df_did, aes(time, mean, group=group, color=group)) +
geom_line(linewidth=1.4) +
geom_point(size=5) +
# Counterfactual dashed line for treated
geom_segment(aes(x="Pre", xend="Post", y=55, yend=53),
linetype=2, color="#0891b2", linewidth=1, inherit.aes=FALSE) +
annotate("text", x=2.05, y=53, label="Counterfactual\n(if no treatment)",
color="#0891b2", size=3.2, hjust=0) +
annotate("segment", x=2, xend=2, y=45, yend=53,
arrow=arrow(length=unit(0.2,"cm"), ends="both"), color="#f59e0b", linewidth=1) +
annotate("text", x=2.05, y=49, label="DiD\nestimate", color="#f59e0b", size=3.5, hjust=0) +
scale_color_manual(values=c("#94a3b8","#e63946")) +
labs(title="DiD: treated group − control group trend = causal effect",
x=NULL, y="Outcome mean", color=NULL) +
theme_di()