From 3e43639605a17b6fb2de334474a6d95342eecc6b Mon Sep 17 00:00:00 2001 From: Mark Colley <39366189+M-Colley@users.noreply.github.com> Date: Sat, 5 Dec 2026 01:51:30 +0000 Subject: [PATCH] Keep marker arrays aligned when dropping missing points Fixes #2497. --- R/plotly_build.R | 27 ++++++++++++++ tests/testthat/test-plotly-marker-alignment.R | 36 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 tests/testthat/test-plotly-marker-alignment.R diff --git a/R/plotly_build.R b/R/plotly_build.R index daf3c86845..731eff56cb 100644 --- a/R/plotly_build.R +++ b/R/plotly_build.R @@ -211,6 +211,8 @@ plotly_build.plotly <- function(p, registerFrames = TRUE) { } if (NROW(builtData) > 0) { + alignMarker <- is_tidy(trace) && !has_group(trace) && is.list(trace[["marker"]]) + if (alignMarker) builtData[[".plotlyRowIndex"]] <- seq_len(NROW(builtData)) # Build the index used to split one "trace" into multiple traces isAsIs <- vapply(builtData, function(x) inherits(x, "AsIs"), logical(1)) isDiscrete <- vapply(builtData, is.discrete, logical(1)) @@ -254,6 +256,16 @@ plotly_build.plotly <- function(p, registerFrames = TRUE) { c(".plotlyTraceIndex", ".plotlyGroupIndex", if (inherits(trace, "plotly_line")) "x") ) + if (alignMarker) { + # Explicit marker arrays describe the same observations as x and y. + rows <- builtData[[".plotlyRowIndex"]] + if (!identical(rows, seq_along(isComplete))) { + trace[["marker"]] <- subset_point_attributes( + trace[["marker"]], Attrs[["marker"]], rows, length(isComplete) + ) + } + builtData[[".plotlyRowIndex"]] <- NULL + } builtData <- train_data(builtData, trace) trace[[".plotlyVariableMapping"]] <- names(builtData) # copy over to the trace data @@ -1026,6 +1038,21 @@ supplyUserPalette <- function(default, user) { # helper functions array_ok <- function(attr) isTRUE(tryNULL(attr$arrayOk)) + +subset_point_attributes <- function(attrs, schema, rows, n) { + for (name in intersect(names(attrs), names(schema))) { + value <- attrs[[name]] + spec <- schema[[name]] + if (array_ok(spec) && is.atomic(value) && is.null(dim(value)) && + length(value) == n) { + attrs[[name]] <- value[rows] + } else if (is.list(value) && is.list(spec)) { + attrs[[name]] <- subset_point_attributes(value, spec, rows, n) + } + } + attrs +} + has_fill <- function(trace) { trace_type <- trace[["type"]] %||% "scatter" # if trace type has fillcolor, but no fill attribute, then fill is always relevant diff --git a/tests/testthat/test-plotly-marker-alignment.R b/tests/testthat/test-plotly-marker-alignment.R new file mode 100644 index 0000000000..3adf828c9b --- /dev/null +++ b/tests/testthat/test-plotly-marker-alignment.R @@ -0,0 +1,36 @@ +test_that("marker arrays follow retained point coordinates", { + colorscale <- list(list(0, "blue"), list(0.5, "purple"), list(1, "green")) + p <- plot_ly() %>% add_trace( + type = "scatter", mode = "markers", + x = c(NA, 2, 3, 4, 5), y = 0, + marker = list( + color = c(0, 0, 0.5, 1, 1), + size = 5:9, + cmin = 0, cmax = 1, colorscale = colorscale, + line = list(color = c("red", "blue", "green", "black", "purple"), width = 2) + ) + ) + + expect_warning(built <- plotly_build(p), "Ignoring 1 observations") + trace <- built$x$data[[1]] + expect_equal(as.numeric(trace$x), 2:5) + expect_equal(as.numeric(trace$marker$color), c(0, 0.5, 1, 1)) + expect_equal(as.numeric(trace$marker$size), 6:9) + expect_equal(as.character(trace$marker$line$color), c("blue", "green", "black", "purple")) + expect_equal(trace$marker$line$width, 2) + expect_equal(trace$marker$colorscale, colorscale) +}) + +test_that("marker arrays stay aligned when observations are split into traces", { + p <- plot_ly( + x = c(10, 20, NA, 40, 50), y = 0, split = c("b", "a", "b", "a", "b"), + type = "scattergl", mode = "markers", marker = list(size = c(1, 2, 3, 4, 5)) + ) + + expect_warning(built <- plotly_build(p), "Ignoring 1 observations") + traces <- setNames(built$x$data, vapply(built$x$data, `[[`, "", "name")) + expect_equal(as.numeric(traces$a$x), c(20, 40)) + expect_equal(as.numeric(traces$a$marker$size), c(2, 4)) + expect_equal(as.numeric(traces$b$x), c(10, 50)) + expect_equal(as.numeric(traces$b$marker$size), c(1, 5)) +})