From 4bcb62864fa6f51a267318797cb764301cfc550e Mon Sep 17 00:00:00 2001 From: Mark Colley <39366189+M-Colley@users.noreply.github.com> Date: Sat, 5 Dec 2026 06:37:09 +0000 Subject: [PATCH] Keep splom dimensions aligned with categorical colors --- R/plotly_build.R | 68 +++++++++++++++++++++-- tests/testthat/test-plotly-splom-colors.R | 64 +++++++++++++++++++++ 2 files changed, 127 insertions(+), 5 deletions(-) create mode 100644 tests/testthat/test-plotly-splom-colors.R diff --git a/R/plotly_build.R b/R/plotly_build.R index daf3c86845..bf144601f9 100644 --- a/R/plotly_build.R +++ b/R/plotly_build.R @@ -225,6 +225,10 @@ plotly_build.plotly <- function(p, registerFrames = TRUE) { # so keep track of which variable it is... trace$frameOrder <- which(names(splitVars) %in% "frame") } + splom_split <- identical(trace[["type"]], "splom") && any(isSplit) + if (splom_split) { + builtData[[".splomRowIndex"]] <- seq_len(nrow(builtData)) + } # Build the index used to determine grouping (later on, NAs are inserted # via group2NA() to create the groups). This is done in 3 parts: # 1. Sort data by the trace index since groups are nested within traces. @@ -254,6 +258,13 @@ plotly_build.plotly <- function(p, registerFrames = TRUE) { c(".plotlyTraceIndex", ".plotlyGroupIndex", if (inherits(trace, "plotly_line")) "x") ) + if (splom_split) { + # Nested point attributes still follow the input row order. + trace[[".splomRows"]] <- list( + index = builtData[[".splomRowIndex"]], n = length(isComplete) + ) + builtData[[".splomRowIndex"]] <- NULL + } builtData <- train_data(builtData, trace) trace[[".plotlyVariableMapping"]] <- names(builtData) # copy over to the trace data @@ -985,9 +996,23 @@ map_linetype <- function(traces) { } +# Recursively subset attributes that have one value per observation. +subset_trace_attr <- function(x, n, idx) { + if (is.list(x)) { + lapply(x, subset_trace_attr, n, idx) + } else if (length(x) == n) { + x[idx] + } else { + x + } +} + + # break up a single trace into multiple traces according to values stored # a particular key name traceify <- function(dat, x = NULL) { + splom_rows <- dat[[".splomRows"]] + dat[[".splomRows"]] <- NULL if (length(x) == 0) return(list(dat)) lvls <- if (is.factor(x)) levels(x) else unique(x) lvls <- lvls[lvls %in% x] @@ -996,19 +1021,38 @@ traceify <- function(dat, x = NULL) { # since that _should_ be the darkest color in a sequential pallette if (is.ordered(x)) lvls <- rev(lvls) n <- length(x) - # recursively search for a non-list of appropriate length (if it is, subset it) - recurse <- function(z, n, idx) { - if (is.list(z)) lapply(z, recurse, n, idx) else if (length(z) == n) z[idx] else z - } new_dat <- list() for (j in seq_along(lvls)) { - new_dat[[j]] <- lapply(dat, function(y) recurse(y, n, x %in% lvls[j])) + idx <- x %in% lvls[j] + new_dat[[j]] <- if (is.null(splom_rows)) { + lapply(dat, subset_trace_attr, n, idx) + } else { + subset_splom_trace(dat, idx, splom_rows$index[idx], splom_rows$n) + } new_dat[[j]]$name <- new_dat[[j]]$name %||% lvls[j] } return(new_dat) } +subset_splom_trace <- function(trace, idx, rows, n) { + mapped <- intersect(names(trace), trace[[".plotlyVariableMapping"]]) + trace[mapped] <- lapply(trace[mapped], subset_trace_attr, length(idx), idx) + trace$dimensions <- lapply(trace$dimensions, function(dimension) { + values <- dimension$values + if (is.atomic(values) && is.null(dim(values)) && length(values) == n) { + # Keep one-observation groups as arrays in the JSON sent to plotly.js. + dimension$values <- I(values[rows]) + } + dimension + }) + trace$marker <- subset_point_attributes( + trace$marker, Schema$traces$splom$attributes$marker, rows, n + ) + trace +} + + eval_attr <- function(x, data = NULL) { if (rlang::is_formula(x)) rlang::eval_tidy(rlang::f_rhs(x), data = data, env = rlang::f_env(x)) else x @@ -1026,6 +1070,20 @@ 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-splom-colors.R b/tests/testthat/test-plotly-splom-colors.R new file mode 100644 index 0000000000..7f9a63ca41 --- /dev/null +++ b/tests/testthat/test-plotly-splom-colors.R @@ -0,0 +1,64 @@ +test_that("Categorical splom colors keep their dimension rows", { + x <- 1:6 + y <- 11:16 + colors <- c("a", "b", "c", "a", "b", "c") + plot <- plot_ly( + type = "splom", color = colors, marker = list(size = y), + dimensions = list(list(label = "X", values = x), list(label = "Y", values = y)) + ) + + traces <- plotly_build(plot)$x$data + expect_identical(vapply(traces, `[[`, character(1), "name"), c("a", "b", "c")) + for (trace in traces) { + rows <- colors == trace$name + expect_identical(as.integer(trace$dimensions[[1]]$values), x[rows]) + expect_identical(as.integer(trace$dimensions[[2]]$values), y[rows]) + labels <- vapply(trace$dimensions, `[[`, character(1), "label") + expect_identical(labels, c("X", "Y")) + expect_identical(as.integer(trace$marker$size), y[rows]) + } +}) + +test_that("Splom dimensions stay arrays when a color group has one row", { + data <- data.frame(x = 1:5, y = 11:15, group = c("a", "b", "a", "c", "b")) + plot <- plot_ly( + data, type = "splom", color = ~group, + dimensions = list(list(label = "X", values = ~x), list(label = "Y", values = ~y)) + ) + + json <- jsonlite::fromJSON( + plotly_json(plot, jsonedit = FALSE), simplifyVector = FALSE + ) + traces <- setNames(json$data, vapply(json$data, `[[`, character(1), "name")) + expect_equal(traces$c$dimensions[[1]]$values, list(4)) + expect_equal(traces$c$dimensions[[2]]$values, list(14)) +}) + +test_that("Splom grouping preserves axis and marker metadata", { + colorscale <- list(list(0, "red"), list(1, "blue")) + plot <- plot_ly( + type = "splom", color = c("b", "a"), + dimensions = list( + list(label = "X", values = c(2, 8), axis = list(range = c(0, 10))), + list(label = "Y", values = c(12, 18), axis = list(range = c(10, 20))) + ), + marker = list( + size = c(11, 21), line = list(width = c(1, 2)), colorscale = colorscale, + colorbar = list(tickvals = c(0, 10), ticktext = c("low", "high")) + ) + ) + + traces <- plotly_build(plot)$x$data + expect_identical(vapply(traces, `[[`, character(1), "name"), c("a", "b")) + sizes <- vapply(traces, function(trace) trace$marker$size, numeric(1)) + widths <- vapply(traces, function(trace) trace$marker$line$width, numeric(1)) + expect_equal(sizes, c(21, 11)) + expect_equal(widths, c(2, 1)) + for (trace in traces) { + expect_equal(trace$dimensions[[1]]$axis$range, c(0, 10)) + expect_equal(trace$dimensions[[2]]$axis$range, c(10, 20)) + expect_identical(trace$marker$colorscale, colorscale) + expect_equal(as.numeric(trace$marker$colorbar$tickvals), c(0, 10)) + expect_identical(as.character(trace$marker$colorbar$ticktext), c("low", "high")) + } +})