R: How to show lines from different plots into a single plot (and creating combinations of those lines)

Viewed 29

I am using the synthetic did package (synthdid) inside a loop to estimate the effects of many different events. The package has a function called synthdid_plot, which plots the trend of the treatment and control observations before and after treatment. My aim is:

  1. to only plot the difference between the two lines (treatment and synthetic control).
  2. store each of these plots in a list in order to make a single plot having, for each event, the line equal to the difference between the treatment and control line.

The issue I am having now is that, once I have stored the plots, I am not able to combine them into a single graph, but I can only create a single figure with the plots one adjacent to the other. Furthermore, I am not able to create the line equal to the difference of the treated and control line.

Here is an example of the code I am working on:

library(patchwork)
plot_list <- list()

for(i in 1:2){ 
  data(california_prop99)
  california_prop99$PacksPerCapita <- california_prop99$PacksPerCapita * i
  california_prop99$date = as.Date(sprintf('%04d/%02d/%02d', california_prop99$Year, 1, 1))
  setup = panel.matrices(california_prop99[! california_prop99$Year %in%  c(1974:1977, 1989:1992),], time='date')
  estimate = synthdid_estimate(setup$Y, setup$N0, setup$T0)
  plot(estimate)
  plot_list[[i]] <- plot(estimate)
}

full_plot <- patchwork::wrap_plots(plot_list, nrow=1)

Thank you in advance for the help!

0 Answers
Related