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.
This commit is contained in:
sim 2021-01-08 16:22:56 -05:00 committed by GitHub
parent fdb6b36506
commit 73b53658e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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",