From 8651b29416c1bf9fe6df7c5e5785df49872e5477 Mon Sep 17 00:00:00 2001 From: Mark Colley <39366189+M-Colley@users.noreply.github.com> Date: Fri, 4 Dec 2026 23:52:06 +0000 Subject: [PATCH] Preserve connected paths with varying tooltips --- NEWS.md | 3 ++ R/ggplotly.R | 20 ++++++++++- tests/testthat/test-geom-path-tooltip.R | 48 +++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 tests/testthat/test-geom-path-tooltip.R diff --git a/NEWS.md b/NEWS.md index 6581d724aa..c042c1b184 100644 --- a/NEWS.md +++ b/NEWS.md @@ -11,6 +11,9 @@ * #2495: `ggplotly()` now keeps merged legend titles in sync with their legend entries, fixing a case where an upstream change in `{rlang}`'s hashing could silently reorder combined legend titles (e.g. `factor(vs)
factor(cyl)`) out of step with the entries they label. +* `ggplotly()` keeps lines and paths connected when their tooltip text varies + by point (#2502). + # plotly 4.12.0 diff --git a/R/ggplotly.R b/R/ggplotly.R index afe8befbf3..6ea4b0c491 100644 --- a/R/ggplotly.R +++ b/R/ggplotly.R @@ -276,7 +276,25 @@ gg2list <- function(p, width = NULL, height = NULL, }, layers, layer_data) # Compute aesthetics to produce data with generalised variable names - data <- by_layer(function(l, d) l$compute_aesthetics(d, plot)) + data <- by_layer(function(l, d) { + computed <- l$compute_aesthetics(d, plot) + if (!inherits(l$geom, "GeomPath") || !is.discrete(computed[["text"]])) { + return(computed) + } + + mapping_names <- names(l$computed_mapping) %||% c( + names(l$mapping), + if (isTRUE(l$inherit.aes)) names(plot$mapping) + ) + if ("group" %in% c(mapping_names, names(l$aes_params))) { + return(computed) + } + + # Keep text for hover without letting it split a path into groups. + grouping_data <- computed[setdiff(names(computed), c("text", "group"))] + computed$group <- ggfun("add_group")(grouping_data)$group + computed + }) if (exists("setup_plot_labels", envir = asNamespace("ggplot2"))) { # Mirror ggplot2/#5879 plot$labels <- ggfun("setup_plot_labels")(plot, layers, data) diff --git a/tests/testthat/test-geom-path-tooltip.R b/tests/testthat/test-geom-path-tooltip.R new file mode 100644 index 0000000000..d81dafbf02 --- /dev/null +++ b/tests/testthat/test-geom-path-tooltip.R @@ -0,0 +1,48 @@ +test_that("varying tooltips do not break a line into single points", { + data <- data.frame(x = c(3, 1, 4, 2), y = 1:4, label = letters[1:4]) + p <- ggplot(data, aes(x, y, text = label)) + geom_line() + traces <- ggplotly(p, tooltip = "text")$x$data + ordered <- data[order(data$x), ] + + expect_length(traces, 1) + expect_equal(traces[[1]]$x, ordered$x) + expect_equal(traces[[1]]$y, ordered$y) + expect_equal(traces[[1]]$text, ordered$label) +}) + +test_that("path tooltips preserve colour-based and explicit grouping", { + data <- data.frame( + x = rep(1:3, times = 2), y = 1:6, + series = rep(c("a", "b"), each = 3), label = letters[1:6] + ) + coords <- function(p) { + lapply(ggplotly(p)$x$data, function(trace) trace[c("x", "y")]) + } + + colour <- ggplot(data, aes(x, y, colour = series)) + line_with_text <- suppressWarnings(geom_line(aes(text = label))) + expect_equal( + coords(colour + line_with_text), + coords(colour + geom_line()) + ) + + grouped <- ggplot(data, aes(x, y, group = series)) + path_with_text <- suppressWarnings(geom_path(aes(text = label))) + expect_equal( + coords(grouped + path_with_text), + coords(grouped + geom_path()) + ) +}) + +test_that("a non-inheriting path ignores the plot's group mapping", { + data <- data.frame(x = c(3, 1, 4, 2), y = 1:4, label = letters[1:4]) + path <- suppressWarnings( + geom_path(aes(x, y, text = label), inherit.aes = FALSE) + ) + p <- ggplot(data, aes(group = label)) + path + traces <- ggplotly(p, tooltip = "text")$x$data + + expect_length(traces, 1) + expect_equal(traces[[1]]$x, data$x) + expect_equal(traces[[1]]$text, data$label) +})