ggplot(panel_prepped)
knitr::opts_chunk$set(warning = F, message = F)library(flipbookr)
Using a difference-in-difference framework to look at the effect of policy interventions is a popular research design. A binary exlanatory variable that turns on and stays on lends itself to ease of interpretation!
In the context of such a design, what is the essential visual inspections? Below I suggest four plots that researchers and their audiences may find useful in visually inspecting the timing of interventions and the relationships between a policy intervention and outcomes.
I use ggplot2 to implement the visualization. The ability to overwrite global aesthetics, using the aes() function, means that we move from one plot to another with little additional code. A little over a year ago I learned about declaring the aes() on it's own line and maybe novelty bias is at work here, but I find the capability to be a lot of fun to play with!
The five plots are as follows:
Another option for visualizing such data is using the new package panelView, which certainly gave me additional inspiration for this exercise and looks useful!
library(tidyverse)library(gapminder)
We'll just use
min_year <- min(gapminder$year)max_year <- max(gapminder$year)span <- max_year - min_yeargapminder %>% select(country) %>% distinct() %>% sample_frac(.5) %>% mutate(intervention_year = runif(n = n(), min = min_year, max = max_year + span)) %>% mutate(intervention_year = ifelse(max_year < intervention_year, NA, intervention_year)) %>% mutate(intervention_year = round(intervention_year)) %>% sample_n(16) ->synthetic_interventions
synthetic_interventions %>% inner_join(gapminder) %>% mutate(treatment = case_when( year >= intervention_year ~ "treated", year < intervention_year ~ "not treated", is.na(intervention_year) ~ "not treated")) %>% group_by(country) %>% mutate(mean_treated = mean(treatment == "treated")) %>% arrange(mean_treated) %>% ungroup() %>% mutate(country = forcats::fct_inorder(as.character(country))) -> panel_prepped
ggplot(panel_prepped)
ggplot(panel_prepped) + aes(x = year)
ggplot(panel_prepped) + aes(x = year) + aes(y = country)
ggplot(panel_prepped) + aes(x = year) + aes(y = country) + aes(group = country)
ggplot(panel_prepped) + aes(x = year) + aes(y = country) + aes(group = country) + labs(caption = "Four essential plots for diff-in-diff | Gina Reynolds")
ggplot(panel_prepped) + aes(x = year) + aes(y = country) + aes(group = country) + labs(caption = "Four essential plots for diff-in-diff | Gina Reynolds") + geom_line(alpha = .5)
ggplot(panel_prepped) + aes(x = year) + aes(y = country) + aes(group = country) + labs(caption = "Four essential plots for diff-in-diff | Gina Reynolds") + geom_line(alpha = .5) + geom_point(size = 1.5)
ggplot(panel_prepped) + aes(x = year) + aes(y = country) + aes(group = country) + labs(caption = "Four essential plots for diff-in-diff | Gina Reynolds") + geom_line(alpha = .5) + geom_point(size = 1.5) + aes(color = treatment)
ggplot(panel_prepped) + aes(x = year) + aes(y = country) + aes(group = country) + labs(caption = "Four essential plots for diff-in-diff | Gina Reynolds") + geom_line(alpha = .5) + geom_point(size = 1.5) + aes(color = treatment) + theme_minimal()
ggplot(panel_prepped) + aes(x = year) + aes(y = country) + aes(group = country) + labs(caption = "Four essential plots for diff-in-diff | Gina Reynolds") + geom_line(alpha = .5) + geom_point(size = 1.5) + aes(color = treatment) + theme_minimal() + scale_color_manual(values = c("lightgrey", "steelblue4"))
ggplot(panel_prepped) + aes(x = year) + aes(y = country) + aes(group = country) + labs(caption = "Four essential plots for diff-in-diff | Gina Reynolds") + geom_line(alpha = .5) + geom_point(size = 1.5) + aes(color = treatment) + theme_minimal() + scale_color_manual(values = c("lightgrey", "steelblue4")) + labs(color = NULL)
ggplot(panel_prepped) + aes(x = year) + aes(y = country) + aes(group = country) + labs(caption = "Four essential plots for diff-in-diff | Gina Reynolds") + geom_line(alpha = .5) + geom_point(size = 1.5) + aes(color = treatment) + theme_minimal() + scale_color_manual(values = c("lightgrey", "steelblue4")) + labs(color = NULL) + labs(x = NULL)
ggplot(panel_prepped) + aes(x = year) + aes(y = country) + aes(group = country) + labs(caption = "Four essential plots for diff-in-diff | Gina Reynolds") + geom_line(alpha = .5) + geom_point(size = 1.5) + aes(color = treatment) + theme_minimal() + scale_color_manual(values = c("lightgrey", "steelblue4")) + labs(color = NULL) + labs(x = NULL) + labs(y = "country")
ggplot(panel_prepped) + aes(x = year) + aes(y = country) + aes(group = country) + labs(caption = "Four essential plots for diff-in-diff | Gina Reynolds") + geom_line(alpha = .5) + geom_point(size = 1.5) + aes(color = treatment) + theme_minimal() + scale_color_manual(values = c("lightgrey", "steelblue4")) + labs(color = NULL) + labs(x = NULL) + labs(y = "country") + labs(title = "Plot 1: Treatment-control summary")
ggplot(panel_prepped) + aes(x = year) + aes(y = country) + aes(group = country) + labs(caption = "Four essential plots for diff-in-diff | Gina Reynolds") + geom_line(alpha = .5) + geom_point(size = 1.5) + aes(color = treatment) + theme_minimal() + scale_color_manual(values = c("lightgrey", "steelblue4")) + labs(color = NULL) + labs(x = NULL) + labs(y = "country") + labs(title = "Plot 1: Treatment-control summary") + # overwriting y position mapping aes(y = gdpPercap) + scale_y_log10() + labs(y = "GDP per cap") + labs(title = "Plot 2: Treatment-control response overlaid summary")
ggplot(panel_prepped) + aes(x = year) + aes(y = country) + aes(group = country) + labs(caption = "Four essential plots for diff-in-diff | Gina Reynolds") + geom_line(alpha = .5) + geom_point(size = 1.5) + aes(color = treatment) + theme_minimal() + scale_color_manual(values = c("lightgrey", "steelblue4")) + labs(color = NULL) + labs(x = NULL) + labs(y = "country") + labs(title = "Plot 1: Treatment-control summary") + # overwriting y position mapping aes(y = gdpPercap) + scale_y_log10() + labs(y = "GDP per cap") + labs(title = "Plot 2: Treatment-control response overlaid summary") + # overwriting x position mapping aes(x = year - intervention_year) + labs(x = "year from intervention year") + labs(title = "Plot 3: Treatment-control response overlaid summary, aligned intervention")
ggplot(panel_prepped) + aes(x = year) + aes(y = country) + aes(group = country) + labs(caption = "Four essential plots for diff-in-diff | Gina Reynolds") + geom_line(alpha = .5) + geom_point(size = 1.5) + aes(color = treatment) + theme_minimal() + scale_color_manual(values = c("lightgrey", "steelblue4")) + labs(color = NULL) + labs(x = NULL) + labs(y = "country") + labs(title = "Plot 1: Treatment-control summary") + # overwriting y position mapping aes(y = gdpPercap) + scale_y_log10() + labs(y = "GDP per cap") + labs(title = "Plot 2: Treatment-control response overlaid summary") + # overwriting x position mapping aes(x = year - intervention_year) + labs(x = "year from intervention year") + labs(title = "Plot 3: Treatment-control response overlaid summary, aligned intervention") + # overwriting x position mapping again aes(x = year) + labs(x = NULL) + scale_y_continuous() + labs(title = "Plot 4: Treatment-Response faceted") + facet_wrap(~ country)
knitr::opts_chunk$set(warning = F, message = F)library(flipbookr)
Using a difference-in-difference framework to look at the effect of policy interventions is a popular research design. A binary exlanatory variable that turns on and stays on lends itself to ease of interpretation!
In the context of such a design, what is the essential visual inspections? Below I suggest four plots that researchers and their audiences may find useful in visually inspecting the timing of interventions and the relationships between a policy intervention and outcomes.
I use ggplot2 to implement the visualization. The ability to overwrite global aesthetics, using the aes() function, means that we move from one plot to another with little additional code. A little over a year ago I learned about declaring the aes() on it's own line and maybe novelty bias is at work here, but I find the capability to be a lot of fun to play with!
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |