diff --git a/DESCRIPTION b/DESCRIPTION index 1b148d89..b765934f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -30,7 +30,8 @@ Imports: slider, utils, lifecycle, - gtable + gtable, + tidyselect Suggests: tsibbledata, pillar (>= 1.0.1), @@ -49,7 +50,7 @@ License: GPL-3 URL: http://feasts.tidyverts.org/, https://github.com/tidyverts/feasts/ BugReports: https://github.com/tidyverts/feasts/issues Encoding: UTF-8 -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.1 Roxygen: list(markdown = TRUE, roclets=c('rd', 'collate', 'namespace')) Language: en-GB RdMacros: lifecycle diff --git a/NAMESPACE b/NAMESPACE index e1552bda..1d767f9a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -50,6 +50,7 @@ export(feat_pacf) export(feat_spectral) export(feat_stl) export(gg_arma) +export(gg_irf) export(gg_lag) export(gg_season) export(gg_subseries) @@ -137,6 +138,7 @@ importFrom(stats,ts) importFrom(stats,var) importFrom(tibble,tibble) importFrom(tidyr,gather) +importFrom(tsibble,as_tsibble) importFrom(utils,tail) importFrom(vctrs,vec_cast) importFrom(vctrs,vec_ptype2) diff --git a/NEWS.md b/NEWS.md index e38aa218..9c84dc17 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,12 @@ # feasts (development version) +## New features + +* Added `gg_irf()` for plotting impulse responses (typically obtained from using + `IRF()` with fable models). + +## Bug fixes + * Fixed `gg_season()` not wrapping across `facet_period` argument correctly. # feasts 0.3.2 diff --git a/R/graphics.R b/R/graphics.R index 1b2ff9c0..6e6d8aeb 100644 --- a/R/graphics.R +++ b/R/graphics.R @@ -805,3 +805,29 @@ gg_arma <- function(data){ ggplot2::coord_fixed(ratio = 1) + facet_grid(vars(!!!fcts), vars(!!sym("type"))) } + + + +#' Plot impulse response functions +#' +#' Produces a plot of impulse responses from an impulse response function. +#' +#' @param data A tsibble with impulse responses +#' @param y The impulse response variables to plot (defaults to all measured variables). +#' +#' @return A ggplot object of the impulse responses. +#' +#' @export +gg_irf <- function(data, y = all_of(measured_vars(data))){ + kv <- key_vars(data) + if(is_empty(kv)) kv <- NULL + data <- tidyr::pivot_longer( + data, {{y}}, + names_to = ".variable", values_to = ".response" + ) + ggplot(data) + + geom_line(ggplot2::aes_string(x = index_var(data), y = ".response")) + + ggplot2::geom_hline(yintercept = 0, linetype = "dashed") + + facet_grid(vars(!!!syms(kv)), vars(!!sym(".variable"))) + + ggplot2::labs(y = "Impulse response", x = NULL) +} diff --git a/man/gg_irf.Rd b/man/gg_irf.Rd new file mode 100644 index 00000000..85644f5f --- /dev/null +++ b/man/gg_irf.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/graphics.R +\name{gg_irf} +\alias{gg_irf} +\title{Plot impulse response functions} +\usage{ +gg_irf(data, y = all_of(measured_vars(data))) +} +\arguments{ +\item{data}{A tsibble with impulse responses} +} +\value{ +A ggplot object of the impulse responses. +} +\description{ +Produces a plot of impulse responses from an impulse response function. +}