Skip to content

Commit

Permalink
Merge pull request facebook#273 from hoxo-m/plot-changepoints
Browse files Browse the repository at this point in the history
Add function to draw significant changepoints
  • Loading branch information
bletham committed Oct 11, 2017
2 parents a43cfe8 + 270bd71 commit 3ce2070
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions utils
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#' Get layers to overlay significant changepoints on prophet forecast plot.
#'
#' @param m Prophet model object.
#' @param threshold Numeric, changepoints where abs(delta) >= threshold are significant. (Default 0.01)
#' @param cp_color Character, line color. (Default "red")
#' @param cp_linetype Character or integer, line type. (Default "dashed")
#' @param trend Logical, if FALSE, do not draw trend line. (Default TRUE)
#' @param ... Other arguments passed on to layers.
#'
#' @return A list of ggplot2 layer.
#'
#' @import ggplot2
#'
#' @examples
#' \dontrun{
#' plot(m, fcst) + layer_changepoints(m)
#' }
#'
#' @export
layer_changepoints <- function(m, threshold = 0.01, cp_color = "red",
cp_linetype = "dashed", trend = TRUE, ...) {
layers <- list()
if (trend) {
trend_layer <- geom_line(aes_string("ds", "trend"), color = cp_color, ...)
layers <- append(layers, trend_layer)
}
signif_changepoints <- m$changepoints[abs(m$params$delta) >= threshold]
cp_layer <- geom_vline(xintercept = as.integer(signif_changepoints),
color = cp_color, linetype = cp_linetype, ...)
layers <- append(layers, cp_layer)
layers
}

0 comments on commit 3ce2070

Please sign in to comment.