Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)<br />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

Expand Down
20 changes: 19 additions & 1 deletion R/ggplotly.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
48 changes: 48 additions & 0 deletions tests/testthat/test-geom-path-tooltip.R
Original file line number Diff line number Diff line change
@@ -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)
})