我希望创建脉冲响应函数模拟的并排图:
library(tsDyn)
data(barry)
mod_var <- lineVar(barry, lag = 2)
irf <- irf(mod_var, impulse = "dolcan",
response = c("dolcan", "cpiUSA", "cpiCAN"), boot = FALSE)
irf1 <- irf(mod_var, impulse = "dolcan", response = "cpiUSA", boot = FALSE)
irf2 <- irf(mod_var, impulse = "cpiCAN", response = "dolcan", boot = FALSE)
单独地,plot(irf1)
和plot(irf2)
给了我想要的情节,但有没有办法将它们并排绘制?
我已经尝试了par(mfrow=c(1,2))
和lattice plot解决方案,但似乎在这种特殊情况下都没有效果。
提供一些额外的细节:我希望将它们绘制成R Notebook
环境中的单个图形,并将markdown文件编织为带有pandoc
的PDF。
答案 0 :(得分:0)
使用ggplot2(以及一点点dplyr)的一种解决方案
library(ggplot2)
library(dplyr)
irf1_df <- data.frame(impulse = "dolcan", response = "cpiUSA", value = irf1$irf$dolcan[ , 1]) %>% mutate(index = 1:n())
irf2_df <- data.frame(impulse = "cpiCAN", response = "dolcan", value = irf2$irf$cpiCAN[ , 1]) %>% mutate(index = 1:n())
irf_df <- rbind(irf1_df, irf2_df)
ggplot(irf_df) +
geom_hline(yintercept = 0, colour = "red") +
geom_line(aes(x = index, y = value, colour = impulse)) +
facet_wrap(~impulse) +
labs(title = "Orthogonal Impulse Response")