Skip to content

Commit 2e3f20d

Browse files
authored
Merge pull request #773 from zeehio/fix_tooltip_on_geom_line_group
Fix wrong tooltip when ggplotly with geom_line(group=)
2 parents 32020c2 + 4d3ac60 commit 2e3f20d

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* When `height`/`width` are specified in `ggplotly()`, relative sizes are now translated correctly. Fixes #489 and #510.
1818
* More careful handling of font when expanding annotation arrays. Fixes #738.
1919
* Ignore data arrays of non-tidy traces. Fixes #737.
20+
* When using `ggplotly()` on a plot with `geom_line` and `group` aesthetic wrong tooltip information was shown. Fixes #774.
2021

2122
# 4.5.5 -- 28 September 2016
2223

R/ggplotly.R

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,19 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all",
174174

175175
# Compute aesthetics to produce data with generalised variable names
176176
data <- by_layer(function(l, d) l$compute_aesthetics(d, plot))
177-
177+
178+
# The computed aesthetic codes the groups as integers
179+
# Here we build a map each of the integer values to the group label
180+
group_maps <- Map(function(x, y) {
181+
tryCatch({
182+
x_group <- x[["group"]]
183+
names(x_group) <- y
184+
x_group <- x_group[!duplicated(x_group)]
185+
x_group
186+
}, error = function(e) NULL
187+
)
188+
}, data, groupDomains)
189+
178190
# Transform all scales
179191
data <- lapply(data, ggfun("scales_transform_df"), scales = scales)
180192

@@ -205,7 +217,17 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all",
205217

206218
# Reparameterise geoms from (e.g.) y and width to ymin and ymax
207219
data <- by_layer(function(l, d) l$compute_geom_1(d))
208-
220+
221+
# compute_geom_1 can reorder the rows from `data`, making groupDomains
222+
# invalid. We rebuild groupDomains based on the current `data` and the
223+
# group map we built before.
224+
groupDomains <- Map(function(x, y) {
225+
tryCatch({
226+
names(y)[match(x$group, y)]
227+
}, error = function(e) NULL
228+
)
229+
}, data, group_maps)
230+
209231
# Apply position adjustments
210232
data <- by_layer(function(l, d) l$compute_position(d, layout))
211233

tests/testthat/test-ggplot-tooltip.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,27 @@ test_that("group domain is included in hovertext", {
6969
expect_true(all(grepl(pattern, txt)))
7070
})
7171

72+
test_that("tooltip elements are not crossed", {
73+
# Tooltips with y == 10 should belong to Sample2 in this example
74+
mydata <- data.frame(id = paste0("Sample", rep(1:2, times = 4)),
75+
x = rep(1:4, each = 2),
76+
y = rep(c(1, 10), times = 4),
77+
stringsAsFactors = FALSE)
78+
# id x y
79+
# 1 Sample1 1 1
80+
# 2 Sample2 1 10
81+
# 3 Sample1 2 1
82+
# 4 Sample2 2 10
83+
# 5 Sample1 3 1
84+
# 6 Sample2 3 10
85+
# 7 Sample1 4 1
86+
# 8 Sample2 4 10
87+
gplt <- ggplot(mydata) + geom_line(aes(x=x, y = y, group = id))
88+
pltly <- plotly::ggplotly(gplt)
89+
y_equal_ten <- grepl("y: 10", pltly$x$data[[1]]$text)
90+
sample_2 <- grepl("id: Sample2", pltly$x$data[[1]]$text)
91+
expect_equal(y_equal_ten, sample_2)
92+
})
7293

7394
labelDF <- data.frame(
7495
label = paste0(("label"), c(1:10)),

0 commit comments

Comments
 (0)