From 73b53658e17f8d092f69fdfb5f7f46ca428959f6 Mon Sep 17 00:00:00 2001 From: sim <38705645+arid-mangoss@users.noreply.github.com> Date: Fri, 8 Jan 2021 16:22:56 -0500 Subject: [PATCH] used regex to format the `name` variable for plotting holiday components (#1766) * used regex to format the `name` Removes all singlequotes (') and doublequotes (") in the `name` variable and replaces all whitespace with an underscore. Now, `ggplot2::aes_string()` can handle a column name like `New Year's Day`. * Used backticks in the `name` variable instead of regular expressions Essentially, I escaped the string `name` with backticks so that `ggplot2::geom_line()` and `ggplot2::ggplot()` functions can use any arbitrary input. --- R/R/plot.R | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/R/R/plot.R b/R/R/plot.R index eb15c5c..3e2194d 100644 --- a/R/R/plot.R +++ b/R/R/plot.R @@ -177,8 +177,17 @@ prophet_plot_components <- function( plot_forecast_component <- function( m, fcst, name, uncertainty = TRUE, plot_cap = FALSE ) { + + wrapped.name <- paste0("`", name, "`") + + lower.name <- paste0(name, '_lower') + lower.name <- paste0("`", lower.name, "`") + + upper.name <- paste0(name, '_upper') + upper.name <- paste0("`", upper.name, "`") + gg.comp <- ggplot2::ggplot( - fcst, ggplot2::aes_string(x = 'ds', y = name, group = 1)) + + fcst, ggplot2::aes_string(x = 'ds', y = wrapped.name, group = 1)) + ggplot2::geom_line(color = "#0072B2", na.rm = TRUE) if (exists('cap', where = fcst) && plot_cap) { gg.comp <- gg.comp + ggplot2::geom_line( @@ -192,7 +201,7 @@ plot_forecast_component <- function( gg.comp <- gg.comp + ggplot2::geom_ribbon( ggplot2::aes_string( - ymin = paste0(name, '_lower'), ymax = paste0(name, '_upper') + ymin = lower.name, ymax = upper.name ), alpha = 0.2, fill = "#0072B2",