So here's a more thorough walkthrough, with the end-result being arranged via gridExtra.
So first of all, while it wasn't included in your code, it seems that you're using the vars and tsDyn packages. My solution relies on purrr, dplyr, tibble, ggplot2, stringr, and tidyr, all of which are part of the tidyverse. So it's easiest just to load the tidyverse, as well as vars, tsDyn and of course gridExtra.
library(tidyverse)
library(vars)
library(tsDyn)
library(gridExtra)
The code you shared
(only change to the code is that I've assigned the data to debt_arg)
Your data
debt_arg <- structure(c(43.0909967382336, 26.9790446803372, 16.967092620984,
29.5051405616314, 4.17318850227895, -4.84876355707382, -24.0607156164264,
-17.9926676757792, -26.8046197351318, -39.2565717944843, -39.308523853837,
-46.8804759131896, -89.3024279725422, -101.794380031895, -106.046332091248,
-121.2682841506, -95.2702362099528, -62.4721882693054, -53.5841403286581,
-38.7460923880107, -22.6580444473633, -42.4199965067158, -1.6919485660685,
46.4060993745788, 27.4541473152261, 172.152195255873, 181.360243196521,
176.898291137168, 309.076339077816, 336.794387018463, 196.02243495911,
395.660482899758, 464.988530840405, 494.556578781053, 396.9146267217,
505.832674662347, 524.940722602995, 519.918770543642, 420.396818484289,
349.224866424937, 186.772914365584, 184.720962306232, 40.1990102468787,
87.1570581875262, 42.2151061281736, -182.786845931179, -248.408797990532,
-223.860750049884, -246.332702109237, -160.484654168589, -130.956606227942,
-53.8285582872948, -69.6505103466475, -113.822462406, -119.124414465353,
-71.5863665247052, -105.228318584058, -64.0802706434106, -111.022222702763,
-40.9141747621159, -34.1361268214685, -92.7280788808212, -6.2500309401737,
37.8680170004736, 60.686064941121, 101.094112881769, 103.382160822416,
-74.1797912369368, -99.1317432962894, -228.933695355642, -534.125647414995,
-654.007599474347, -688.0995515337, -617.091503593053, -501.913455652405,
-462.655407711758, -405.577359771111, -539.759311830463, -464.641263889816,
-370.263215949168, -338.035168008521, -361.017120067874, -495.649072127226,
-418.951024186579, -349.762976245931, -393.074928305284, -331.696880364637,
-305.778832423989, -232.140784483342, -150.822736542695, -116.134688602047,
-30.4666406614001, -5.22859272075266, 86.9494552198946, 34.457503160542,
108.705551101189, 158.723599041837, 141.341646982484, 55.3496949231317,
-67.5222571362212, 72.7557908044264, 129.453838745074, 106.911886685721,
222.349934626368, 289.877982567016, 368.006030507663, 454.244078448311,
670.382126388958, 506.110174329605, 573.968222270253, 755.1862702109,
363.624318151547, 664.462366092195, 414955.426174104, 410044.891543103,
403034.35691006, 412023.822277016, 422013.287643972, 418102.753010928,
435092.218377884, 438381.68374484, 432171.149111795, 445760.614478752,
450850.079845708, 445939.545212663, 432029.01057962, 418218.475946575,
394307.941313531, 372097.406680487, 346586.872047443, 319376.337414399,
287765.802781355, 250555.268148311, 228444.733515267, 192634.198882223,
142523.664249179, 114613.129616135, 73902.594983091, 52092.060350047,
28781.5257170029, 13770.9910839589, -7139.54354908504, -31950.0781821292,
-44460.6128151731, -55771.1474482173, -75681.6820812612, -101592.216714305,
-120802.751347349, -125313.285980393, -155323.820613438, -190834.355246481,
-219044.889879525, -244455.42451257, -247865.959145613, -249076.493778658,
-245687.028411702, -244097.563044746, -271308.09767779, -282518.632310834,
-301029.166943878, -301139.701576922, -301850.236209966, -287760.77084301,
-300871.305476054, -316081.840109098, -312692.374742142, -328102.909375186,
-330013.44400823, -348623.978641274, -380734.513274318, -393545.047907362,
-409355.582540406, -429866.11717345, -451376.651806494, -483687.186439538,
-509197.721072583, -531308.255705626, -548218.79033867, -577929.324971715,
-593639.859604758, -618950.394237803, -628560.928870847, -622271.463503891,
-592781.998136935, -507492.532769979, -501703.067403023, -474413.602036067,
-482224.136669111, -509334.671302155, -507745.205935199, -506355.740568243,
-473266.275201287, -424176.809834331, -399587.344467375, -322497.879100419,
-207608.413733463, -169418.948366507, -145829.482999551, -99540.0176325953,
-18950.5522656394, 8438.91310131643, 27528.3784682727, 34117.8438352286,
87907.3092021844, 137896.77456914, 196686.239936097, 236275.705303052,
240065.170670008, 288854.636036964, 325344.10140392, 340733.566770876,
296223.032137832, 353212.497504788, 431101.962871744, 455291.4282387,
519680.893605656, 551470.358972612, 591359.824339568, 664649.289706524,
697038.75507348, 720028.220440436, 703717.685807392, 697007.151174348,
717496.616541304, 716186.08190826, 728275.547275216, 1538.70118346597,
2398.42391964878, 2392.09965578383, 4133.94339191895, 3777.72612805406,
4867.33386418917, 4108.55160032429, 5550.94433645941, 4634.90307259452,
5278.04380872964, 5088.51254486475, 7486.71828099987, 6869.96001713498,
6070.3357532701, 4563.65148940522, 6128.08522554033, 2788.93596167545,
2682.28969781056, 2229.84943394568, 3741.65917008079, 2630.38390621591,
4082.16364235103, 2198.15237848614, 4896.45411462126, 4157.35585075637,
4921.13358689149, 4820.0463230266, 6147.74805916172, 4105.90179529683,
5193.46153143195, 3666.34326756707, 4597.95400370218, 1570.8447398373,
2221.92547597241, 166.215212107527, 1763.06994824264, -1176.90431562224,
-961.478579487128, -1735.65784335201, -1407.9491072169, -5683.92737108178,
-5080.73563494666, -8825.40289881155, -6968.02316267644, -17166.0654265413,
-20479.0666904062, -20975.0449542711, -19596.572218136, -18925.8824820009,
-16613.6967458657, -16307.3730097306, -14033.8542735955, -13841.2785374604,
-12716.7748013253, -13487.4250651902, -11195.5563290551, -12067.9225929199,
-10807.5008567848, -10885.5221206497, -7810.93638451459, -10139.4426483795,
-8558.90691224435, -8606.53717610924, -5071.93643997413, -6154.21470383901,
-3857.6179677039, -5539.78023156878, 47.5015045663386, -3501.44675929855,
-156.083023163432, 610.628712971687, 761.724449106798, -6687.22581475809,
-6513.97907862297, -6552.17734248786, -671.552606352736, -4200.09687021763,
-2435.65713408251, -933.942397947387, 10092.1733381877, 5412.08207432284,
7628.10081045795, 7935.10854659307, 19198.7752827282, 11920.7060188633,
17229.0047549984, 16831.0094911335, 31239.1312272686, 19334.4489634038,
20324.2716995389, 20205.076435674, 26013.2221718091, 3328.73490794423,
5991.50064407934, 6570.13238021445, 20323.7811163496, 11933.7938524847,
15170.6605886198, 16821.4173247549, 13656.38206089, -8733.33720297486,
-4910.67546683973, -7102.64173070462, 6088.80900543051, 663.179741565611,
1739.09747770072, 216.726213835747, 8738.72494997097, -3065.35931389393,
-12117.1165777588, -29574.4198416237, -23324.9971054886, -32336.0323693535,
270.48, 265.24, 652.73, 800.62, 747.79, 660.42, 419.8, 426.33,
420.66, 412.91, 487.09, 581.92, 558.75, 519.1, 604.55, 460.49,
382.46, 405.58, 431.74, 518.96, 509.36, 607.4, 558.86, 649.37,
706.08, 809.41, 822.47, 687.5, 709.97, 550.44, 380.28, 430.06,
419.78, 498.72, 534.4, 550.47, 569.24, 496.9, 475.42, 416.77,
443.81, 402.25, 243.55, 295.39, 436.13, 350.65, 395.19, 524.95,
566.46, 765.61, 827.69, 1071.96, 1201.66, 945.45, 1142.5, 1375.37,
1400.42, 1367.41, 1694.83, 1543.31, 1800.58, 1711.09, 1637.27,
2090.46, 2102.78, 2190.87, 2187.97, 2151.73, 2103.72, 2107.87,
1598.17, 1079.66, 1125.95, 1587.97, 2075.14, 2320.73, 2373.71,
2185.01, 2643.42, 3523.59, 3388.03, 3360.64, 2463.63, 2462.63,
2683.99, 2346.68, 2451.73, 2854.29, 3380.78, 2976.27, 4783.77,
5391.03, 6373.82, 7887.33, 12548.99, 8597.02, 10837.23, 11656.81,
9814.62, 11675.18, 12992.43, 14683.49, 16675.68, 16917.86, 20265.32,
21912.63, 26078.29, 30065.61, 31108.86, 26037.01, 33461.77, 30292.55,
33466.03, 3.26e+08, 2.1e+07, 1.1e+07, 7.869e+09, 862806000, 538167500,
476328500, 157505500, 731948111.111111, 24350957088.2353, 999682500,
4203927088.23529, 1806496171.11111, 164884528.235294, 2178749500,
2576536163.86555, -1104639488.88889, 1183937108.57143, 1323054635.48522,
2240739607.7745, 3231203531.20524, 2224656303.03667, 2799743875.36837,
2852285791.5461, 4248938759.38433, 1762499191.62355, 2765832512.19452,
1498329486.02275, 3436536228.02919, 4467813458.46352, 2844847427.10581,
153779772.547889, 768245951.027719, 2874603185.30815, 84905815.7334787,
2265403119.14626, 2444297843.45995, 1369023915.06914, -253538732.407444,
-1664050535.17686, -2690301300.23777, -1596956359.91031, -2925302741.22981,
-2533781832.48488, -2258880000, -1220300000, 142670000, -1664830000,
-2036900000, -2134950000, -497270000, -3059160000, -3754980000,
-1132040000, -2212520000, -2153150000, -3101240000, -728950000,
991330000, 1156170000, 2753260645.58738, 1910464819.74444, 852896475.94148,
2277112718.5433, 2022087685.81889, 5273568557.49061, -1502521294.35984,
1063441737.14022, -226518951.379118, -1253194355.76109, -2177171727.73634,
-1535518403.57232, 96147441.6142862, -296349372.551493, -627558644.109443,
-467930782.724932, 405257699.151905, 144634171.908078, 337338547.911262,
1859566805.68781, 796934898.126637, 286707726.576741, -236299720.277408,
-887533002.579341, 1553120015.26578, 27003236.9796999, -1454448585.92745,
124239641.731611, 168476951.081102, -568202127.603569, -253500911.561899,
1240493061.28016, 1162373441.80884, -4349136882.03693, 540850191.216397,
63371068.2983998, 1134211263.74247, 1367030943.20801, 618885142.028415,
-2829317497.3944, 4728966886.53824, 12590576935.5312, 6879440409.39944,
10868932518.4022, 15553076839.5202, 4374233548.15403, 6179975808.64763,
12303491605.5898, 16348265006.8077, -1264432453.55569, -1071543953.94089,
-728949104.179213, 519284160.862738, 0, 0, 0, 0, 321395635.417275,
-27163751.5253196, 578208608.58746, 341330056.878433, 253612251.627469,
407474140.566744, 3485218027.37565, 1524948883.60027, 1095063503.93567,
-243667941.737244, 2303448152.30374, 1065446917.68978, 637179622.619144,
414989651.653276, 57287900.1825424, 442961505.748743, 614615523.155122,
474062261.101183, -528254493.861979, 306917257.290977, 466868012.120676,
91074750.1073804, 1873300034.41525, -111963691.48737, 562133833.756524,
-499826456.669203, -65715454.514262, -206227359.32734, -95642879.0800113,
-10545930096.3757, -309843542.700806, 178698848.75935, 281636981.229176,
-94039364.237304, -3456799000.38897, 42016298.5163033, 29986452.0184,
-34745156.95985, -69089531.8771083, 104975191.0695, 3880000,
18160000, -1.2e+07, -125920000, 49020000, 19960000, -67760000,
64130000, -60730000, 32580000, -11850000, -46150000, -3340000,
-34610000, -67770000, 57620000, 247065572.305654, 110723963.408605,
132149695.892849, 216716497.314816, 549906355.874947, 594931451.444728,
228472127.27772, 411625076.952925, -92909673.1325036, -202873018.788848,
-58179792.5150349, -176659291.195054, -73100388.2223938, 10404172.6882247,
-54286803.4483821, -94934845.5623686, -215442207.120147, -82511407.2007654,
95972418.060002, 130964874.692201, -51017265.9840873, 85839986.5801663,
126620883.760985, -369639265.12419, -29215440.7294261, -11121692.0314486,
-115981397.706632, 21410812.1712994, 79654137.3840613, 27308434.4325477,
-4229545.44216412, 239144758.016801, 14686931.9932968, 231275172.573391,
66295018.1798717, -94357525.4859095, 41697675.4063964, 22833247.3850187,
-44701305.1208689, 219281935.220203, 29548818.0680997, 38598506.304463,
417521774.71664, 499547569.881022, 50027913.0234979, 852409184.207933,
1596119677.49392, 514743198.781707, 102026577.988947, -292454396.807919,
-130885581.879457, -187033564.604513, 195110655.737268), .Dim = c(113L,
6L), .Dimnames = list(NULL, c("sp", "m1_us", "m1_arg", "eq_arg",
"pfdebt_arg", "pfequity_arg")), .Tsp = c(1991, 2019, 4), class = c("mts",
"ts", "matrix"))
Your computation
## Estimate VAR Model
var.est.debt_arg <- VAR(debt_arg,p=1,type="both",season=NULL)
summary(var.est.debt_arg)
## Impulse Response Functions
debtarg_1 <- irf(var.est.debt_arg,response="pfdebt_arg",impulse="sp",n.ahead=40,ortho=TRUE,boot=TRUE)
# response of pfdebt to s&p shock
debtarg_2 <- irf(var.est.debt_arg,response="pfdebt_arg",impulse="m1_us",n.ahead=40,ortho=TRUE,boot=TRUE)
# response of pfdebt to us M1 Shock
debtarg_3 <- irf(var.est.debt_arg,response="pfdebt_arg",impulse="m1_arg",n.ahead=40,ortho=TRUE,boot=TRUE)
# response of pfdebt to a domestic m1 shock
debtarg_4 <- irf(var.est.debt_arg,response="pfdebt_arg",impulse="eq_arg",n.ahead=40,ortho=TRUE,boot=TRUE)
# response of pfdebt to equity market price shock
debtarg_5 <- irf(var.est.debt_arg,response="pfdebt_arg",impulse="pfequity_arg",n.ahead=40,ortho=TRUE,boot=TRUE)
# response of pfdebt to pfequity shocks
Wrangling the data from varirf objects to tidy dataframes
bound <- list(debtarg_1, debtarg_2, debtarg_3, debtarg_4, debtarg_5) %>%
map(., `[`, 1:3) %>% # This has to do accessing the data as vectors rather than a bunch of varirf objects
unlist(., recursive = FALSE) %>%
bind_cols(.) %>%
rownames_to_column(var = "n") %>% # This column will refer to the periods going forward (n.ahead)
pivot_longer(-n) %>% #this relies on the new tidyr package
mutate(n = as.numeric(n)) # for later on when we plot it
ready <- bound %>%
mutate(distinction = case_when(as.numeric(str_extract(name, ".$")) == 1 ~ "Lower bound", # varirf denotes the lower bound with 1
as.numeric(str_extract(name, ".$")) == 2 ~ "Upper bound", # varirf denotes the upper bound with 2
TRUE ~ "Point estimate")) %>% # Any name that doesn't end with 1 or 2 is the point estimate
mutate(name = if_else(str_detect(name, "us"), # As a convenience ...
"us", # I'm renaming the US M1 money supply shock to "us".
name), # That way we can do the next thing...
name = str_extract(name, "^.{2}"), #Which is to keep only the first two letters of each name.
linetype_var = if_else(str_detect(distinction, "bound"), # Here we distinguish the point estimate from the up/low bounds
"2",
"1"))
Plotting the whole thing
So to create a list of plots, we first split our dataframe into a list of dataframes. We then apply a function consisting of our ggplot code to each dataframe in the list using the map function from the purrr package.
# We create a list of plots by resplitting the data into lists
# And apply a function consisting of our ggplot call to each dataframe in the list
plot_list <- ready %>%
group_by(name) %>%
group_split() %>%
map(function(.) {ggplot(., aes(x = n, y = value, color = linetype_var, linetype = linetype_var, group = distinction)) +
geom_line() +
facet_wrap(~name) +
theme_bw() +
theme(legend.position = "none") +
scale_color_manual(values = c("red", "black"))})
# Here's where gridExtra comes in
marrangeGrob(plot_list, nrow = 5, ncol = 1)
And this is what we get.
Hope that answers the question. Don't hesitate to follow up if somethings unclear (or if I've misunderstood your intentions). And welcome to the RStudio Community board 