R/autoplot.varirf.R

Defines functions autoplot autoplot.varirf

Documented in autoplot autoplot.varirf

# create methods
#' @export
autoplot <- function(x, path, file_name, device) UseMethod("autoplot", x)

#' @export
#' @name autoplot
#' @title autoplot
#' @description plot all impulse response functions with their impulses and responses beautifully with ggplot2. Additionally you can save them directly by specifying a path.
#' @param irf varirf object, which is to be plotted.
#' @param path character, specifying the path where the plots are safed.
#' @param file_name character, specifying the prefix of the final plot names.
#' @param device, character, specifying the output type, default is "pdf"
#' @param res, integer, specifying the resolution of the output files
#' @examples
#'
#' library("FredApi")
#' library("vars")
#' library("beautyIRF")
#'
#' x1 <- get_symbols("AURUKM")
#' x2 <- get_symbols("LRHUTTTTITM156S")
#' x3 <- get_symbols("LRHUTTTTFRM156S")
#'
#' x1 <- x1["2000-01-01::2016-01-01"]
#' x2 <- x2["2000-01-01::2016-01-01"]
#' x3 <- x3["2000-01-01::2016-01-01"]
#'
#' x <- data.frame(x1, x2, x3)
#'
#' VAR1 <- VAR(x, lag.max = 12, type = "cons")
#'
#' irf <- vars::irf(VAR1, n.ahead = 20)
#'
#' autoplot(irf)
autoplot.varirf <- function(irf, path = getwd(), file_name, device = "pdf", res = 900) {
  j <- 1
  for (j in 1:length(irf$response)) {
    pp <- list()
    for (i in 1:length(irf$impulse)) {
      irf1 <- data.frame(irf$irf[[i]][, j],
                         irf$Lower[[i]][, j],
                         irf$Upper[[i]][, j])
      irf1$lags <- c(1:nrow(irf1))

      pp[[i]] <- ggplot2::ggplot(data = irf1, ggplot2::aes(lags, irf.irf..i.....j.)) +
        ggplot2::geom_line(ggplot2::aes(y = irf.Upper..i.....j.), colour = "blue") +
        ggplot2::geom_line(ggplot2::aes(y = irf.Lower..i.....j.), colour = "blue")+
        ggplot2::geom_line(ggplot2::aes(y = irf.irf..i.....j.))+
        ggplot2::geom_ribbon(ggplot2::aes(x = lags,
                        ymax = irf.Upper..i.....j.,
                        ymin = irf.Lower..i.....j.),
                    fill = "blue",
                    alpha = .1) +
        ggplot2::ggtitle(paste0("Impulse : ", irf$impulse[i])) +
        ggplot2::xlab("Lags") +
        ggplot2::ylab("") +
        ggplot2::geom_hline(yintercept = 0) +
        ggthemes::theme_hc() +
        ggplot2::theme(plot.title = ggplot2::element_text(size = 12))
    }

    if (irf$cumulative == FALSE) {
      title <- paste0("Orthonogal Impulse Response Functions \n Response : ",
                      irf$response[j])
    } else {
      title <- paste0("Cumulative Impulse Response Functions \n Response : ",
                      irf$response[j])
    }

    gridExtra::grid.arrange(grobs = pp,
                 nrow = length(pp),
                 top = grid::textGrob(title, gp = grid::gpar(fontsize = 20)))
    if (!missing(file_name)) {
      ggplot2::ggsave(file = paste0(path, "/", file_name, "_", irf$response[j], ".", device),
             gridExtra::arrangeGrob(grobs = pp,
                         nrow = length(pp),
                         top = grid::textGrob(paste0("Orthonogal Impulse Response Functions \n Response : ",
                                               irf$response[j]), gp = grid::gpar(fontsize = 20))),
             dpi = res,
             width = 16,
             height = 9)
    }
  }
}
markushhh/beautyIRF documentation built on Feb. 14, 2020, 9:21 a.m.