From d553b1b4d91df4724be51ef1689e5ebae2bc4187 Mon Sep 17 00:00:00 2001 From: Kush Patel <56747429+kshptl@users.noreply.github.com> Date: Wed, 2 Dec 2026 14:30:04 +0000 Subject: [PATCH] Fix ribbon fills when a bound is missing Signed-off-by: Kush Patel <56747429+kshptl@users.noreply.github.com> --- R/layers2traces.R | 41 +++++++++++++++- R/plotly_build.R | 2 +- tests/testthat/test-ggplot-ribbon.R | 32 +++++++++++++ tests/testthat/test-plotly-ribbon.R | 74 +++++++++++++++++++++++++++++ 4 files changed, 147 insertions(+), 2 deletions(-) create mode 100644 tests/testthat/test-plotly-ribbon.R diff --git a/R/layers2traces.R b/R/layers2traces.R index 181ef72909..45d02fc797 100644 --- a/R/layers2traces.R +++ b/R/layers2traces.R @@ -1081,7 +1081,46 @@ make_error <- function(data, params, xy = "x") { # function to transform geom_ribbon data into format plotly likes # (note this function is also used for geom_smooth) -ribbon_dat <- function(dat) { +ribbon_dat <- function(dat, connectgaps = FALSE) { + # Let Plotly.js bridge gaps along each edge of a single polygon. + if (isTRUE(connectgaps)) return(ribbon_polygon(dat)) + + missing <- is.na(dat[["ymin"]]) | is.na(dat[["ymax"]]) + if (!any(missing)) return(ribbon_polygon(dat)) + + if (".plotlyGroupIndex" %in% names(dat)) { + group <- ".plotlyGroupIndex" + series <- c(".plotlyTraceIndex", group) + } else { + group <- "group" + series <- c("PANEL", "frame", group) + } + series <- intersect(series, names(dat)) + order_by <- dat[c(series, "x")] + if (".plotlyTraceIndex" %in% series) { + # traceify() draws character traces in their first appearance in x order. + trace <- dat[[".plotlyTraceIndex"]] + order_by[[".plotlyTraceIndex"]] <- match(trace, trace[order(dat[["x"]])]) + } + rows <- do.call(order, order_by) + dat <- dat[rows, , drop = FALSE] + missing <- missing[rows] + + # Give each complete run its own group so group2NA() closes it at the gap. + series_id <- vctrs::vec_group_id(dat[series]) + first <- !duplicated(series_id) + dat[[group]] <- cumsum(missing | first) + + # Keep a blank row for an empty series so an animation can clear its trace. + empty <- first & !series_id %in% series_id[!missing] + dat[["ymin"]][empty] <- NA + dat[["ymax"]][empty] <- NA + dat <- dat[!missing | empty, , drop = FALSE] + polygons <- lapply(split(dat, dat[[group]]), ribbon_polygon) + structure(do.call(rbind, polygons), class = oldClass(dat)) +} + +ribbon_polygon <- function(dat) { n <- nrow(dat) if (n == 0) return(dat) o <- order(dat[["x"]]) diff --git a/R/plotly_build.R b/R/plotly_build.R index daf3c86845..c7973333f2 100644 --- a/R/plotly_build.R +++ b/R/plotly_build.R @@ -556,7 +556,7 @@ registerFrames <- function(p, frameMapping = NULL) { train_data <- function(data, trace) { if (inherits(trace, "plotly_ribbon")) { - data <- ribbon_dat(data) + data <- ribbon_dat(data, connectgaps = trace[["connectgaps"]]) } if (inherits(trace, "plotly_segment")) { # TODO: this could be faster, more efficient diff --git a/tests/testthat/test-ggplot-ribbon.R b/tests/testthat/test-ggplot-ribbon.R index e900a759c9..b053085bed 100644 --- a/tests/testthat/test-ggplot-ribbon.R +++ b/tests/testthat/test-ggplot-ribbon.R @@ -48,3 +48,35 @@ test_that("geom_ribbon() with fill aesthetic produces multiple traces", { # 10 traces -- one for each decade info <- expect_traces(p4, 10, "fill") }) + +test_that("geom_ribbon() starts a new polygon after missing bounds", { + df <- data.frame( + x = 1:6, + ymin = c(1, 2, NA, 4, 5, 6), + ymax = c(11, 12, 13, NA, 15, 16) + ) + p <- ggplot(df, aes(x, ymin = ymin, ymax = ymax)) + geom_ribbon() + trace <- plotly_build(p)$x$data[[1]] + + expect_equal(as.numeric(trace$x), c(1, 2, 2, 2, 1, 1, NA, 5, 6, 6, 6, 5, 5)) + expect_equal(as.numeric(trace$y), c(1, 2, 2, 12, 11, 1, NA, 5, 6, 6, 16, 15, 5)) +}) + +test_that("geom_ribbon() clears the ribbon in a frame with missing bounds", { + df <- data.frame( + x = rep(1:3, 2), + ymin = c(1:3, rep(NA_real_, 3)), + ymax = c(4:6, rep(NA_real_, 3)), + stage = rep(c("complete", "missing"), each = 3) + ) + p <- ggplot(df, aes(x, ymin = ymin, ymax = ymax, frame = stage)) + + geom_ribbon() + frames <- as.list(plotly_build(p)$x$frames) + names(frames) <- vapply(frames, function(frame) frame$name, character(1)) + + expect_identical(names(frames), c("complete", "missing")) + expect_true(any(!is.na(frames[["complete"]]$data[[1]][["y"]]))) + expect_identical(as.integer(frames[["missing"]]$traces), 0L) + y <- frames[["missing"]]$data[[1]][["y"]] + expect_true(length(y) > 0L && all(is.na(y))) +}) diff --git a/tests/testthat/test-plotly-ribbon.R b/tests/testthat/test-plotly-ribbon.R new file mode 100644 index 0000000000..6f6619bf72 --- /dev/null +++ b/tests/testthat/test-plotly-ribbon.R @@ -0,0 +1,74 @@ +test_that("add_ribbons() splits missing bounds within each group", { + df <- data.frame( + group = c(rep("a", 6), rep("b", 2)), + x = c(1:6, 3:4), + ymin = c(1, 2, NA, 4, 5, 6, 0, 0), + ymax = c(11, 12, 13, NA, 15, 16, 1, 1) + ) + df <- dplyr::group_by(df[c(5, 4, 1, 7, 6, 2, 3, 8), ], group) + p <- add_ribbons(plot_ly(df), x = ~x, ymin = ~ymin, ymax = ~ymax) + trace <- plotly_build(p)$x$data[[1]] + + expect_equal(trace$fill, "toself") + expect_equal( + as.numeric(trace$x), + c(1, 2, 2, 2, 1, NA, 5, 6, 6, 6, 5, NA, 3, 4, 4, 4, 3) + ) + expect_equal( + as.numeric(trace$y), + c(1, 2, 2, 12, 11, NA, 5, 6, 6, 16, 15, NA, 0, 0, 0, 1, 1) + ) +}) + +test_that("add_ribbons() connects gaps along each ribbon edge", { + df <- data.frame( + x = 1:5, + ymin = c(0, 0, NA, 0, 0), + ymax = c(1, 1, NA, 1, 1) + ) + p <- add_ribbons(plot_ly(df), x = ~x, ymin = ~ymin, ymax = ~ymax, + connectgaps = TRUE) + trace <- plotly_build(p)$x$data[[1]] + valid <- !is.na(trace$y) + + expect_true(trace$connectgaps) + expect_equal(as.numeric(trace$x[valid]), c(1, 2, 4, 5, 5, 5, 4, 2, 1)) + expect_equal(as.numeric(trace$y[valid]), c(0, 0, 0, 0, 0, 1, 1, 1, 1)) +}) + +test_that("add_ribbons() clears the ribbon in a frame with missing bounds", { + df <- data.frame( + x = rep(1:3, 2), + ymin = c(1:3, rep(NA_real_, 3)), + ymax = c(4:6, rep(NA_real_, 3)), + stage = rep(c("complete", "missing"), each = 3) + ) + p <- add_ribbons(plot_ly(df), x = ~x, ymin = ~ymin, ymax = ~ymax, + frame = ~stage) + frames <- as.list(plotly_build(p)$x$frames) + names(frames) <- vapply(frames, function(frame) frame$name, character(1)) + + expect_identical(names(frames), c("complete", "missing")) + expect_true(any(!is.na(frames[["complete"]]$data[[1]][["y"]]))) + expect_identical(as.integer(frames[["missing"]]$traces), 0L) + y <- frames[["missing"]]$data[[1]][["y"]] + expect_true(length(y) > 0L && all(is.na(y))) +}) + +test_that("add_ribbons() keeps the trace order when a bound is missing", { + df <- data.frame( + x = c(2:5, 1:4), + ymin = 1:8, + ymax = 11:18, + series = rep(c("a", "b"), each = 4) + ) + trace_names <- function(data) { + p <- add_ribbons(plot_ly(data), x = ~x, ymin = ~ymin, ymax = ~ymax, + color = ~series) + vapply(plotly_build(p)$x$data, function(trace) trace$name, character(1)) + } + + expect_identical(trace_names(df), c("b", "a")) + df$ymin[3] <- NA_integer_ + expect_identical(trace_names(df), c("b", "a")) +})