Visualizing Difference in Differences Studies

As someone who studies state and federal policies, I spend a lot of time trying to identify natural experiments that can provide a degree of causal insight into the effects of certain policy choices. Many studies suffer from a lack of causal design and cannot go beyond simply finding correlations, which may be biased by the confounding effects of unmeasured variables. In light of this, researchers seeking to inform policies at the state and federal level are constantly on the lookout for opportunities to use quasi-experimental study designs that take advantage of natural experiments that pseudo-randomly create groups for comparison.

A workhorse of quasi-experimental research is the “difference-in-differences” study. This approach allows researchers to compare a control group and an intervention group (i.e. one impacted by a policy) before and after a change takes place. The purpose of this post isn’t to explain DD, so for anyone who wants to learn more, I’ve included some links to resources at the end of this post. Instead, what I’m interested in showing here is an approach using the gganimate package that can be helpful in visualizing some aspects of DD studies.

Often these analyses include a line plot of the outcome of interest over time for the intervention and control groups, noting the policy change timing with a vertical line. These plots (ideally) show two things. First, that the treatment and control groups were trending similarly prior to the policy change. And second, that there was some change in the trajectory of the group that was exposed to the policy change occurring right at the time of the change. Both of these lend themselves to animation of changes over time.

In ggplot2 using gganimate, this is straightforward, and you can create interpretable DD plots without much fuss. The animation I’ve put together below as an example presents thinner, lighter lines for each of the units in the analysis along with thicker solid lines that show the group averages over time. The vertical line is there, as we’d expect, for the time of the change, and the lines move together leading up to the change before diverging when the change takes place. Keep in mind this is simply demo data from a DD example in R. I’ve included the code below so you can recreate this at home, dig more into the underlying data structure, and apply it to your own analyses!


Make this at home!


# see this example
mydata <- read.dta("")

# create the treated variable
mydata$treated <- factor(if_else(mydata$country %in% c("E","F","G"),1,0)) 

# get summary data for group averages
mydata %>% 
  group_by(year,treated) %>% 
  summarize(y=mean(y)) -> sumdata

ggplot() + geom_line(data=mydata,aes(x=year,y=y,group=country,color=treated),
                     size=1,alpha=0.25) + # plot the individual lines
            size=2) + # plot the averages for each group
  geom_vline(xintercept = 1994) + # intervention point
  scale_x_continuous(breaks=unique(mydata$year)) +
  scale_color_manual(values=c("red","blue"), # label our groups
                     labels=c("Control Average","Treatment Average")) +
  labs(title="Difference in Differences Animation in R",
       subtitle="Using ggplot2 & gganimate",
       caption="Example data from:") +
  theme_minimal() +
  theme(axis.text.y = element_blank(),
        legend.title = element_blank(),
        legend.position = "bottom") +

anim_save(filename = "ddanim.gif")

Difference in Differences Resources

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: