Plot Function

with.overlay <- function(est, s) { attr(est,'overlay') = s; est }

estimators <- function(sdid,sc,s) {
  estimator.list = list(with.overlay(sdid, s), sc)
  names(estimator.list)=c('SDID+Fixed', 'SC')
  estimator.list
}

plot.estimators <- function(ests, alpha.multiplier) {
  p = synthdid_plot(ests, se.method='none',
                    alpha.multiplier=alpha.multiplier, facet=rep(1,length(ests)),
                    trajectory.linetype = 1, effect.curvature=-.4,
                    trajectory.alpha=1, effect.alpha=.5, diagram.alpha=1)
  suppressMessages(p + scale_alpha(range=c(0,1), guide='none'))
}

#Xlabel show
x.br<-seq.Date(as.Date("2000-01-01"),as.Date("2015-01-01"),by="5 year") 
x.lb<-format(x.br,"%Y-%m")

Import Dataset

Wage<-read.csv("Total Quarterly Wages (State).csv")

# Get Date from Quarter,since it can direcly used for analysis
Wage <- Wage%>%
  mutate(Date = case_when(Qtr == 1 ~ paste0(Year, "-01-01"),
                          Qtr == 2 ~ paste0(Year, "-04-01"),
                          Qtr == 3 ~ paste0(Year, "-07-01"),
                          Qtr == 4 ~ paste0(Year, "-10-01"),
                          TRUE ~ "NA"))%>%
  mutate(Date=as.Date(Date),
         treat = ifelse(State == "Louisiana " & #Makes Louisiana as treated State
                        Date >= as.Date("2005-08-01"), 1, 0))

wg<-Wage%>%filter(Date>="2000-01-01",Date<="2015-01-01")
wg<-data.frame(wg)

Total Employement

wg$lg<-log(wg$Total.Covered.Total..all.industries) # makes a log

#Remove State if NA Available
wgp<-wg%>%
  group_by(State)%>%
  filter(!any(is.na(Total.Covered.Total..all.industries)))%>%
  data.frame()

pnm<-panel.matrices(panel = wgp,
                    unit = "State",      #ID Variable    
                    time = "Date",       #Time Period   
                    outcome = "lg",      #Dependent Variable   
                    treatment = "treat") #Treated Dummy 

sdid.x<-synthdid_estimate(pnm$Y,pnm$N0,pnm$T0)
sc.x<-sc_estimate(pnm$Y,pnm$N0,pnm$T0)

p4 <- plot.estimators(estimators(sdid = sdid.x, sc = sc.x, s = 1),
                      alpha.multiplier = c(1, 1, 1))

plot.theme <- theme(legend.position="bottom",
                    legend.direction = 'horizontal',
                    legend.key = element_blank(),
                    legend.background = element_blank(),
                    plot.title = element_text(hjust = 0.5, size = 14,face = "bold"),
                    axis.text = element_text(size = 10),axis.title = element_text(size = 9),
                    )

pl.0 <- p4 + plot.theme +
  labs(title = "",
       y = "Total Employment", x = "Date")+  
  geom_vline(xintercept = as.Date("2005-08-01"),linetype=2)+
  scale_y_continuous(labels = function(x) format(x, scientific = FALSE))+ ## Remove E from Hight Number
  scale_x_continuous(breaks = x.br,
                     labels = x.lb)
 
pl.0

Good Producing

wg$lg<-(wg$Private.Goods.producing)

#Remove State if NA Available
wgp<-wg%>%
  group_by(State)%>%
  filter(!any(is.na(Private.Goods.producing)))%>%
  data.frame()

pnm<-panel.matrices(panel = wgp,
                    unit = "State",      #ID Variable
                    time = "Date",       #Time Period 
                   outcome = "lg",      #Dependent Variable 
                    treatment = "treat") #Treated Dummy

sdid.x<-synthdid_estimate(pnm$Y,pnm$N0,pnm$T0)
sc.x<-sc_estimate(pnm$Y,pnm$N0,pnm$T0)

p4 <- plot.estimators(estimators(sdid = sdid.x, sc = sc.x, s = 1),
                      alpha.multiplier = c(1, 1, 1))

plot.theme <- theme(legend.position="bottom",
                    legend.direction = 'horizontal',
                    legend.key = element_blank(),
                    legend.background = element_blank(),
                    plot.title = element_text(hjust = 0.5, size = 14,face = "bold"),
                    axis.text = element_text(size = 10),axis.title = element_text(size = 9),
                    )

pl.1 <- p4 + plot.theme +
  labs(title = "",
       y = "Good Producing", x = "Date")+  
  geom_vline(xintercept = as.Date("2005-08-01"),linetype=2)+
  scale_y_continuous(labels = function(x) format(x, scientific = FALSE))+
  scale_x_continuous(breaks = x.br,
                     labels = x.lb)
 
pl.1

Natural Resources and Mining

wg$lg<-(wg$Private.Natural.resources.and.mining)

#Remove State if NA Available
wgp<-wg%>%
  group_by(State)%>%
  filter(!any(is.na(Private.Natural.resources.and.mining)))%>%
  data.frame()

pnm<-panel.matrices(panel = wgp,
                    unit = "State",      #ID Variable
                    time = "Date",       #Time Period 
                   outcome = "lg",      #Dependent Variable 
                    treatment = "treat") #Treated Dummy

sdid.x<-synthdid_estimate(pnm$Y,pnm$N0,pnm$T0)
sc.x<-sc_estimate(pnm$Y,pnm$N0,pnm$T0)

p4 <- plot.estimators(estimators(sdid = sdid.x, sc = sc.x, s = 1),
                      alpha.multiplier = c(1, 1, 1))

plot.theme <- theme(legend.position="bottom",
                    legend.direction = 'horizontal',
                    legend.key = element_blank(),
                    legend.background = element_blank(),
                    plot.title = element_text(hjust = 0.5, size = 14,face = "bold"),
                    axis.text = element_text(size = 10),axis.title = element_text(size = 9),
                    )

pl.2 <- p4 + plot.theme +
  labs(title = "",
       y = "Natural Resources and Mining", x = "Date")+  
  geom_vline(xintercept = as.Date("2005-08-01"),linetype=2)+
  scale_y_continuous(labels = function(x) format(x, scientific = FALSE))+
  scale_x_continuous(breaks = x.br,
                     labels = x.lb)
 
pl.2

Construction

wg$lg<-(wg$Private.Construction)

#Remove State if NA Available
wgp<-wg%>%
  group_by(State)%>%
  filter(!any(is.na(Private.Construction)))%>%
  data.frame()

pnm<-panel.matrices(panel = wgp,
                    unit = "State",      #ID Variable
                    time = "Date",       #Time Period 
                   outcome = "lg",      #Dependent Variable 
                    treatment = "treat") #Treated Dummy

sdid.x<-synthdid_estimate(pnm$Y,pnm$N0,pnm$T0)
sc.x<-sc_estimate(pnm$Y,pnm$N0,pnm$T0)

p4 <- plot.estimators(estimators(sdid = sdid.x, sc = sc.x, s = 1),
                      alpha.multiplier = c(1, 1, 1))

plot.theme <- theme(legend.position="bottom",
                    legend.direction = 'horizontal',
                    legend.key = element_blank(),
                    legend.background = element_blank(),
                    plot.title = element_text(hjust = 0.5, size = 14,face = "bold"),
                    axis.text = element_text(size = 10),axis.title = element_text(size = 9),
                    )

pl.3 <- p4 + plot.theme +
  labs(title = "",
       y = "Construction", x = "Date")+  
  geom_vline(xintercept = as.Date("2005-08-01"),linetype=2)+
  scale_y_continuous(labels = function(x) format(x, scientific = FALSE))+
  scale_x_continuous(breaks = x.br,
                     labels = x.lb)
 
pl.3

Manufacturing

wg$lg<-(wg$Private.Manufacturing)

#Remove State if NA Available
wgp<-wg%>%
  group_by(State)%>%
  filter(!any(is.na(Private.Manufacturing)))%>%
  data.frame()

pnm<-panel.matrices(panel = wgp,
                    unit = "State",      #ID Variable
                    time = "Date",       #Time Period 
                   outcome = "lg",      #Dependent Variable 
                    treatment = "treat") #Treated Dummy

sdid.x<-synthdid_estimate(pnm$Y,pnm$N0,pnm$T0)
sc.x<-sc_estimate(pnm$Y,pnm$N0,pnm$T0)

p4 <- plot.estimators(estimators(sdid = sdid.x, sc = sc.x, s = 1),
                      alpha.multiplier = c(1, 1, 1))

plot.theme <- theme(legend.position="bottom",
                    legend.direction = 'horizontal',
                    legend.key = element_blank(),
                    legend.background = element_blank(),
                    plot.title = element_text(hjust = 0.5, size = 14,face = "bold"),
                    axis.text = element_text(size = 10),axis.title = element_text(size = 9),
                    )

pl.4 <- p4 + plot.theme +
  labs(title = "",
       y = "Manufacturing", x = "Date")+  
  geom_vline(xintercept = as.Date("2005-08-01"),linetype=2)+
  scale_y_continuous(labels = function(x) format(x, scientific = FALSE))+
  scale_x_continuous(breaks = x.br,
                     labels = x.lb)
 
pl.4

Service providing

wg$lg<-(wg$Private.Service.providing)

#Remove State if NA Available
wgp<-wg%>%
  group_by(State)%>%
  filter(!any(is.na(Private.Service.providing)))%>%
  data.frame()

pnm<-panel.matrices(panel = wgp,
                    unit = "State",      #ID Variable
                    time = "Date",       #Time Period 
                   outcome = "lg",      #Dependent Variable 
                    treatment = "treat") #Treated Dummy

sdid.x<-synthdid_estimate(pnm$Y,pnm$N0,pnm$T0)
sc.x<-sc_estimate(pnm$Y,pnm$N0,pnm$T0)

p4 <- plot.estimators(estimators(sdid = sdid.x, sc = sc.x, s = 1),
                      alpha.multiplier = c(1, 1, 1))

plot.theme <- theme(legend.position="bottom",
                    legend.direction = 'horizontal',
                    legend.key = element_blank(),
                    legend.background = element_blank(),
                    plot.title = element_text(hjust = 0.5, size = 14,face = "bold"),
                    axis.text = element_text(size = 10),axis.title = element_text(size = 9),
                    )

pl.5 <- p4 + plot.theme +
  labs(title = "",
       y = "Service providing", x = "Date")+  
  geom_vline(xintercept = as.Date("2005-08-01"),linetype=2)+
  scale_y_continuous(labels = function(x) format(x, scientific = FALSE))+
  scale_x_continuous(breaks = x.br,
                     labels = x.lb)
 
pl.5

Trade transportation and utilities

wg$lg<-(wg$Private.Trade..transportation..and.utilities)

#Remove State if NA Available
wgp<-wg%>%
  group_by(State)%>%
  filter(!any(is.na(Private.Trade..transportation..and.utilities)))%>%
  data.frame()

pnm<-panel.matrices(panel = wgp,
                    unit = "State",      #ID Variable
                    time = "Date",       #Time Period 
                   outcome = "lg",      #Dependent Variable 
                    treatment = "treat") #Treated Dummy

sdid.x<-synthdid_estimate(pnm$Y,pnm$N0,pnm$T0)
sc.x<-sc_estimate(pnm$Y,pnm$N0,pnm$T0)

p4 <- plot.estimators(estimators(sdid = sdid.x, sc = sc.x, s = 1),
                      alpha.multiplier = c(1, 1, 1))

plot.theme <- theme(legend.position="bottom",
                    legend.direction = 'horizontal',
                    legend.key = element_blank(),
                    legend.background = element_blank(),
                    plot.title = element_text(hjust = 0.5, size = 14,face = "bold"),
                    axis.text = element_text(size = 10),axis.title = element_text(size = 9),
                    )

pl.6 <- p4 + plot.theme +
  labs(title = "",
       y = "Trade transportation and utilities", x = "Date")+  
  geom_vline(xintercept = as.Date("2005-08-01"),linetype=2)+
  scale_y_continuous(labels = function(x) format(x, scientific = FALSE))+
  scale_x_continuous(breaks = x.br,
                     labels = x.lb)
 
pl.6

Financial activities

wg$lg<-(wg$Private.Financial.activities)

#Remove State if NA Available
wgp<-wg%>%
  group_by(State)%>%
  filter(!any(is.na(Private.Financial.activities)))%>%
  data.frame()

pnm<-panel.matrices(panel = wgp,
                    unit = "State",      #ID Variable
                    time = "Date",       #Time Period 
                   outcome = "lg",      #Dependent Variable 
                    treatment = "treat") #Treated Dummy

sdid.x<-synthdid_estimate(pnm$Y,pnm$N0,pnm$T0)
sc.x<-sc_estimate(pnm$Y,pnm$N0,pnm$T0)

p4 <- plot.estimators(estimators(sdid = sdid.x, sc = sc.x, s = 1),
                      alpha.multiplier = c(1, 1, 1))

plot.theme <- theme(legend.position="bottom",
                    legend.direction = 'horizontal',
                    legend.key = element_blank(),
                    legend.background = element_blank(),
                    plot.title = element_text(hjust = 0.5, size = 14,face = "bold"),
                    axis.text = element_text(size = 10),axis.title = element_text(size = 9),
                    )

pl.7 <- p4 + plot.theme +
  labs(title = "",
       y = "Financial activities", x = "Date")+  
  geom_vline(xintercept = as.Date("2005-08-01"),linetype=2)+
  scale_y_continuous(labels = function(x) format(x, scientific = FALSE))+
  scale_x_continuous(breaks = x.br,
                     labels = x.lb)
 
pl.7

Professional and business services

wg$lg<-(wg$Private.Professional.and.business.services)

#Remove State if NA Available
wgp<-wg%>%
  group_by(State)%>%
  filter(!any(is.na(Private.Professional.and.business.services)))%>%
  data.frame()

pnm<-panel.matrices(panel = wgp,
                    unit = "State",      #ID Variable
                    time = "Date",       #Time Period 
                   outcome = "lg",      #Dependent Variable 
                    treatment = "treat") #Treated Dummy

sdid.x<-synthdid_estimate(pnm$Y,pnm$N0,pnm$T0)
sc.x<-sc_estimate(pnm$Y,pnm$N0,pnm$T0)

p4 <- plot.estimators(estimators(sdid = sdid.x, sc = sc.x, s = 1),
                      alpha.multiplier = c(1, 1, 1))

plot.theme <- theme(legend.position="bottom",
                    legend.direction = 'horizontal',
                    legend.key = element_blank(),
                    legend.background = element_blank(),
                    plot.title = element_text(hjust = 0.5, size = 14,face = "bold"),
                    axis.text = element_text(size = 10),axis.title = element_text(size = 9),
                    )

pl.8 <- p4 + plot.theme +
  labs(title = "",
       y = "Professional and business services", x = "Date")+  
  geom_vline(xintercept = as.Date("2005-08-01"),linetype=2)+
  scale_y_continuous(labels = function(x) format(x, scientific = FALSE))+
  scale_x_continuous(breaks = x.br,
                     labels = x.lb)
 
pl.8

Education and health services

wg$lg<-(wg$Private.Education.and.health.services)

#Remove State if NA Available
wgp<-wg%>%
  group_by(State)%>%
  filter(!any(is.na(Private.Education.and.health.services)))%>%
  data.frame()

pnm<-panel.matrices(panel = wgp,
                    unit = "State",      #ID Variable
                    time = "Date",       #Time Period 
                   outcome = "lg",      #Dependent Variable 
                    treatment = "treat") #Treated Dummy

sdid.x<-synthdid_estimate(pnm$Y,pnm$N0,pnm$T0)
sc.x<-sc_estimate(pnm$Y,pnm$N0,pnm$T0)

p4 <- plot.estimators(estimators(sdid = sdid.x, sc = sc.x, s = 1),
                      alpha.multiplier = c(1, 1, 1))

plot.theme <- theme(legend.position="bottom",
                    legend.direction = 'horizontal',
                    legend.key = element_blank(),
                    legend.background = element_blank(),
                    plot.title = element_text(hjust = 0.5, size = 14,face = "bold"),
                    axis.text = element_text(size = 10),axis.title = element_text(size = 9),
                    )

pl.9 <- p4 + plot.theme +
  labs(title = "",
       y = "Education and health services", x = "Date")+  
  geom_vline(xintercept = as.Date("2005-08-01"),linetype=2)+
  scale_y_continuous(labels = function(x) format(x, scientific = FALSE))+
  scale_x_continuous(breaks = x.br,
                     labels = x.lb)
 
pl.9

Leisure and hospitality

wg$lg<-(wg$Private.Leisure.and.hospitality)

#Remove State if NA Available
wgp<-wg%>%
  group_by(State)%>%
  filter(!any(is.na(Private.Leisure.and.hospitality)))%>%
  data.frame()

pnm<-panel.matrices(panel = wgp,
                    unit = "State",      #ID Variable
                    time = "Date",       #Time Period 
                   outcome = "lg",      #Dependent Variable 
                    treatment = "treat") #Treated Dummy

sdid.x<-synthdid_estimate(pnm$Y,pnm$N0,pnm$T0)
sc.x<-sc_estimate(pnm$Y,pnm$N0,pnm$T0)

p4 <- plot.estimators(estimators(sdid = sdid.x, sc = sc.x, s = 1),
                      alpha.multiplier = c(1, 1, 1))

plot.theme <- theme(legend.position="bottom",
                    legend.direction = 'horizontal',
                    legend.key = element_blank(),
                    legend.background = element_blank(),
                    plot.title = element_text(hjust = 0.5, size = 14,face = "bold"),
                    axis.text = element_text(size = 10),axis.title = element_text(size = 9)
                    )

pl.10 <- p4 + plot.theme +
  labs(title = "",
       y = "Leisure and hospitality", x = "Date")+  
  geom_vline(xintercept = as.Date("2005-08-01"),linetype=2)+
  scale_y_continuous(labels = function(x) format(x, scientific = FALSE))+
  scale_x_continuous(breaks = x.br,
                     labels = x.lb)
 
pl.10

Local Government

wg$lg<-(wg$Local.Government.Total..all.industries)

#Remove State if NA Available
wgp<-wg%>%
  group_by(State)%>%
  filter(!any(is.na(Local.Government.Total..all.industries)))%>%
  data.frame()

pnm<-panel.matrices(panel = wgp,
                    unit = "State",      #ID Variable
                    time = "Date",       #Time Period 
                   outcome = "lg",      #Dependent Variable 
                    treatment = "treat") #Treated Dummy

sdid.x<-synthdid_estimate(pnm$Y,pnm$N0,pnm$T0)
sc.x<-sc_estimate(pnm$Y,pnm$N0,pnm$T0)

p4 <- plot.estimators(estimators(sdid = sdid.x, sc = sc.x, s = 1),
                      alpha.multiplier = c(1, 1, 1))

plot.theme <- theme(legend.position="bottom",
                    legend.direction = 'horizontal',
                    legend.key = element_blank(),
                    legend.background = element_blank(),
                    plot.title = element_text(hjust = 0.5, size = 14,face = "bold"),
                    axis.text = element_text(size = 10),axis.title = element_text(size = 9),
                    )

pl.11 <- p4 + plot.theme +
  labs(title = "",
       y = "Local Government", x = "Date")+  
  geom_vline(xintercept = as.Date("2005-08-01"),linetype=2)+
  scale_y_continuous(labels = function(x) format(x, scientific = FALSE))+
  scale_x_continuous(breaks = x.br,
                     labels = x.lb)
 
pl.11

Arrange Figure

# extract legend Name
lge <- lapply(list(pl.0),
    function(p) {
  # Extract the legend from the plot
  g_legend <- function(p) {
    tmp <- ggplotGrob(p)
    leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
    legend <- tmp$grobs[[leg]]
    return(legend)
  }
  g_legend(p)
})

#Arrange The Plot
main_plot <- grid.arrange(
    pl.0 + theme(legend.position = "none") + xlab(""),
    pl.1 + theme(legend.position = "none") + xlab(""),
    pl.2 + theme(legend.position = "none") + xlab(""),
    pl.3 + theme(legend.position = "none") + xlab(""),
    pl.4 + theme(legend.position = "none") + xlab(""),
    pl.5 + theme(legend.position = "none") + xlab(""),
    pl.6 + theme(legend.position = "none") + xlab(""),
    pl.7 + theme(legend.position = "none") + xlab(""),
    pl.8 + theme(legend.position = "none") + xlab(""),
    pl.9 + theme(legend.position = "none") + xlab(""),
    pl.10 + theme(legend.position = "none") + xlab(""),
    pl.11 + theme(legend.position = "none") + xlab(""), nrow = 4,
    left = textGrob("Total Quarterly Wage", rot = 90,
        gp = gpar(fontface = "bold", fontsize = 18)),
    bottom = textGrob("Date", gp = gpar(fontface = "bold", fontsize = 18)))

grid.arrange(main_plot, lge[[1]], nrow = 2, heights = c(20, 1))