diff --git a/NEWS.md b/NEWS.md index 6581d724aa..1570200263 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,8 @@ +# plotly (development version) + +* Fixed numeric color mappings in scatterplot matrices (`splom`) so the + colorbar appears on the matrix trace (#2500). + # plotly 4.12.1 * Completed the `plot_ly()` documentation for what happens when `type` is not diff --git a/R/plotly_build.R b/R/plotly_build.R index daf3c86845..e86110b57b 100644 --- a/R/plotly_build.R +++ b/R/plotly_build.R @@ -288,7 +288,12 @@ plotly_build.plotly <- function(p, registerFrames = TRUE) { ) for (i in x$.plotlyVariableMapping) { # try to reduce the amount of data we have to send for non-positional scales - entry <- if (i %in% npscales()) uniq(d[[i]]) else d[[i]] + entry <- d[[i]] + # Numeric splom colors can supply a shared scale even when constant in + # this trace. Keep one color value per point in that case. + splom_color <- identical(i, "color") && identical(x[["type"]], "splom") && + is.numeric(entry) && !inherits(x[[i]], "AsIs") + if (i %in% npscales() && !splom_color) entry <- uniq(entry) if (is.null(entry)) { x[[i]] <- NULL } else { @@ -698,6 +703,7 @@ map_color <- function(traces, stroke = FALSE, title = "", colorway, na.color = " # color/colorscale/colorbar attribute placement depends on trace type and marker mode # TODO: remove these and make numeric colorscale mapping more like the rest types <- vapply(traces, function(tr) tr$type %||% "scatter", character(1)) + splom_colors <- !stroke && all(types == "splom") modes <- vapply(traces, function(tr) tr$mode %||% "lines", character(1)) hasLine <- has_line(types, modes) hasLineColor <- has_color_array(types, "line") @@ -801,20 +807,20 @@ map_color <- function(traces, stroke = FALSE, title = "", colorway, na.color = " rng <- range(allColor, na.rm = TRUE) colScale <- scales::col_numeric(pal, rng, na.color = na.color) # generate the colorscale to be shared across traces - vals <- if (diff(rng) > 0) { + varies <- diff(rng) > 0 + vals <- if (varies) { seq(rng[1], rng[2], length.out = 25) } else { c(0, 1) } - colorScale <- matrix( - c(scales::rescale(vals), toRGB(colScale(vals), alphas[[1]])), - ncol = 2 - ) + make_colorscale <- function(alpha) { + matrix(c(scales::rescale(vals), toRGB(colScale(vals), alpha)), ncol = 2) + } colorObj <- list( colorbar = lapply(list(title = as.character(title), ticklen = 2), default), cmin = default(rng[1]), cmax = default(rng[2]), - colorscale = default(colorScale), + colorscale = default(make_colorscale(alphas[[1]])), showscale = default(FALSE) ) for (i in which(isNumeric)) { @@ -830,17 +836,25 @@ map_color <- function(traces, stroke = FALSE, title = "", colorway, na.color = " # if trace is singular (i.e., one unique color in this trace), then there # is no need for a colorscale, and both stroke/color have relevancy - if (isSingular[[i]]) { + if (isSingular[[i]] && !(splom_colors && varies)) { col <- colScale(uniq(color[[i]])) traces[[i]] <- mapColor(traces[[i]], toRGB(col, alphas[[i]])) } else { - colorObj$color <- default(color[[i]]) + mapped_color <- color[[i]] + if (splom_colors && length(mapped_color) == 1L) { + mapped_color <- I(rep(mapped_color, splom_point_count(traces[[i]]))) + } + colorObj$color <- default(mapped_color) if (stroke) { traces[[i]]$marker$line <- modify_list(colorObj, traces[[i]]$marker$line) } else { - traces[[i]]$marker <- modify_list(colorObj, traces[[i]]$marker) + marker <- colorObj + if (splom_colors && isSingular[[i]] && !identical(alphas[[i]], alphas[[1]])) { + marker$colorscale <- default(make_colorscale(alphas[[i]])) + } + traces[[i]]$marker <- modify_list(marker, traces[[i]]$marker) } if (hasLine[[i]]) { @@ -873,6 +887,11 @@ map_color <- function(traces, stroke = FALSE, title = "", colorway, na.color = " # exit early if no additional colorbar trace is needed if (any(hasZ)) return(traces) if (stroke && sum(lengths(lapply(traces, "[[", "stroke"))) == 0) return(traces) + + # A Cartesian colorbar trace introduces axes outside the splom matrix. + if (splom_colors) { + return(map_splom_colorbar(traces, which(isNumeric))) + } # add an "empty" trace with the colorbar colorObj$color <- rng @@ -905,6 +924,46 @@ map_color <- function(traces, stroke = FALSE, title = "", colorway, na.color = " traces } +splom_point_count <- function(trace) { + dims <- Filter(function(dim) !identical(dim[["visible"]], FALSE), trace$dimensions) + sizes <- lengths(lapply(dims, `[[`, "values")) + sizes <- sizes[sizes > 0L] + if (length(sizes)) min(sizes) else 1L +} + +map_splom_colorbar <- function(traces, indices) { + shown <- vapply(traces[indices], function(trace) { + !any(trace[["visible"]] %in% c(FALSE, "legendonly")) + }, logical(1)) + indices <- indices[shown] + markers <- lapply(traces[indices], `[[`, "marker") + local_scale <- vapply(markers, function(marker) { + is.null(marker[["coloraxis"]]) && is.numeric(marker[["color"]]) + }, logical(1)) + visible <- local_scale & vapply(markers, function(marker) { + isTRUE(marker[["showscale"]]) + }, logical(1)) + + if (!any(visible)) { + automatic <- local_scale & vapply(markers, function(marker) { + is.default(marker[["showscale"]]) + }, logical(1)) + customized <- vapply(markers, function(marker) { + user_specified(marker[["colorbar"]]) + }, logical(1)) + first <- c(which(automatic & customized), which(automatic))[1L] + if (!is.na(first)) { + traces[[indices[[first]]]]$marker$showscale <- default(TRUE) + visible[[first]] <- TRUE + } + } + + for (i in indices[visible]) { + traces[[i]] <- prefix_class(traces[[i]], "plotly_colorbar") + } + traces +} + map_symbol <- function(traces) { symbolList <- lapply(traces, "[[", "symbol") nSymbols <- lengths(symbolList) diff --git a/R/utils.R b/R/utils.R index b94a9a2ef6..f756d108af 100644 --- a/R/utils.R +++ b/R/utils.R @@ -885,15 +885,12 @@ verify_showlegend <- function(p) { verify_guides <- function(p) { - # since colorbars are implemented as "invisible" traces, prevent a "trivial" legend - if (has_colorbar(p) && has_legend(p) && length(p$x$data) <= 2) { + # Suppress a trivial legend for a plot with a separate colorbar trace. + isBar <- vapply(p$x$data, is_visible_colorbar, logical(1)) + splom <- vapply(p$x$data, function(tr) identical(tr[["type"]], "splom"), logical(1)) + if (any(isBar & !splom) && has_legend(p) && length(p$x$data) <= 2) { p$x$layout$showlegend <- default(FALSE) } - - isVisibleBar <- function(tr) { - is.colorbar(tr) && (tr$showscale %||% TRUE) - } - isBar <- vapply(p$x$data, isVisibleBar, logical(1)) nGuides <- sum(isBar) + has_legend(p) if (nGuides > 1) { @@ -980,11 +977,13 @@ has_legend <- function(p) { isTRUE(p$x$layout$showlegend %|D|% TRUE) } -has_colorbar <- function(p) { - isVisibleBar <- function(tr) { - is.colorbar(tr) && isTRUE(tr$showscale %||% TRUE) +is_visible_colorbar <- function(tr) { + if (!is.colorbar(tr)) return(FALSE) + if (identical(tr[["type"]], "splom")) { + isTRUE(as.logical(tr$marker[["showscale"]])) + } else { + isTRUE(tr$showscale %||% TRUE) } - any(vapply(p$x$data, isVisibleBar, logical(1))) } # is a given trace type 3d? diff --git a/tests/testthat/test-plotly-splom.R b/tests/testthat/test-plotly-splom.R index 5580bff1ee..4499af3ea0 100644 --- a/tests/testthat/test-plotly-splom.R +++ b/tests/testthat/test-plotly-splom.R @@ -10,5 +10,59 @@ test_that("No cartesian axes are supplied to a splom chart", { list(values=c(2,5,6), label="B") ) ) - + expect_false(any(c("xaxis", "yaxis") %in% names(plotly_build(p)$x$layout))) +}) + +test_that("numeric colors use a splom marker colorbar", { + values <- c(1, 3, 5) + p <- plot_ly( + type = "splom", + dimensions = list( + list(values = c(1, 2, 3), label = "A"), + list(values = c(2, 5, 6), label = "B") + ), + color = values + ) + + expect_warning(p <- plotly_build(p), NA) + built <- p$x + expect_length(built$data, 1) + expect_false(any(c("xaxis", "yaxis") %in% names(built$layout))) + expect_equal(as.numeric(built$data[[1]]$marker$color), values) + expect_true(as.logical(built$data[[1]]$marker$showscale)) + + modified <- colorbar(p, title = "Score", limits = c(2, 4))$x$data[[1]]$marker + expect_equal(modified$colorbar$title, "Score") + expect_equal(as.numeric(modified$color), c(NA, 3, NA)) + expect_false(as.logical(hide_colorbar(p)$x$data[[1]]$marker$showscale)) +}) + +test_that("multiple splom traces share a numeric scale and retain their legends", { + dimensions <- list( + list(values = c(1, 2, 3), label = "A"), + list(values = c(2, 5, 6), label = "B") + ) + p <- plot_ly( + type = "splom", dimensions = dimensions, color = rep(1, 3), name = "First", + marker = list(showscale = FALSE) + ) + p <- add_trace( + p, type = "splom", dimensions = dimensions, color = 5, name = "Second", + marker = list(colorbar = list(title = "Shared score")), inherit = FALSE + ) + + expect_warning(built <- plotly_build(p)$x, NA) + expect_equal(vapply(built$data, `[[`, character(1), "type"), rep("splom", 2)) + expect_true(as.logical(built$layout$showlegend)) + + first <- built$data[[1]]$marker + second <- built$data[[2]]$marker + expect_false(first$showscale) + expect_true(as.logical(second$showscale)) + expect_equal(second$colorbar$title, "Shared score") + expect_equal(as.numeric(first$color), rep(1, 3)) + expect_equal(as.numeric(second$color), rep(5, 3)) + expect_equal(first$colorscale, second$colorscale) + expect_equal(as.numeric(c(first$cmin, first$cmax)), c(1, 5)) + expect_equal(as.numeric(c(second$cmin, second$cmax)), c(1, 5)) })