From 3f3ce99899f20195a8f355fb357e84477549475c Mon Sep 17 00:00:00 2001 From: Mitchell O'Hara-Wild Date: Tue, 4 Feb 2025 19:08:50 +1100 Subject: [PATCH] Fix auto[plot/layer] draw key for single-point multiple-forecast ribbons Resolves #414 --- NEWS.md | 5 +++++ R/plot.R | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/NEWS.md b/NEWS.md index 4c2bcd8f..20726166 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,10 @@ # fabletools (development version) +## Bug fixes + +* Fixed forecast autoplot() and autolayer() draw key for single-point + multiple-forecast ribbons (#414). + # fabletools 0.5.0 ## New features diff --git a/R/plot.R b/R/plot.R index b547871c..3fcf7b9f 100644 --- a/R/plot.R +++ b/R/plot.R @@ -316,6 +316,9 @@ build_fbl_layer <- function(object, data = NULL, level = c(80, 95), if (is.null(data[["fill"]]) && (!is.null(data[["fill_ramp"]]) || !all(is.na(data[["alpha"]])))) { data$fill = "gray65" } + if (is.null(data[["colour"]]) && (!is.null(data[["colour_ramp"]]) || !all(is.na(data[["alpha"]])))) { + data$colour = "black" + } # Apply ramped fill if (!is.null(data[["fill_ramp"]])) { if (utils::packageVersion("ggdist") > "3.3.1") { @@ -326,6 +329,16 @@ build_fbl_layer <- function(object, data = NULL, level = c(80, 95), }, data$fill, data$fill_ramp) } } + # Apply ramped colour + if (!is.null(data[["colour_ramp"]])) { + if (utils::packageVersion("ggdist") > "3.3.1") { + data$colour <- get("ramp_colours", asNamespace("ggdist"), mode = "function")(data$colour, data$colour_ramp) + } else { + data$colour <- mapply(function(color, amount){ + (scales::seq_gradient_pal(attr(amount, "from") %||% "white", color))(amount %||% NA) + }, data$colour, data$colour_ramp) + } + } ggplot2::draw_key_rect(data, params, size) }