Skip to content

Commit c11110e

Browse files
committed
Fix #2415: variable named 'group' now shown in tooltip
1 parent 73bd179 commit c11110e

3 files changed

Lines changed: 21 additions & 2 deletions

File tree

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
## Bug fixes
88

99
* `plotly_build()` now works with `ggmatrix` objects (e.g., from `GGally::ggpairs()`). (#2447)
10+
* Closed #2415: `ggplotly()` now shows variables named 'group' in tooltips when mapped to aesthetics like `colour`.
1011

1112
# plotly 4.11.0
1213

R/layers2traces.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ layers2traces <- function(data, prestats_data, layout, p) {
5656
if (!aesName %in% names(x)) next
5757
# TODO: should we be getting the name from scale_*(name) first?
5858
varName <- y[[i]]
59-
# "automatically" generated group aes is not informative
60-
if (identical("group", unique(varName, aesName))) next
59+
# Skip auto-generated group aesthetic (where both aes name and var name are "group"),
60+
# but allow: (1) variables named "group" mapped to other aesthetics like colour,
61+
# and (2) other variables mapped to the "group" aesthetic
62+
if (identical(aesName, "group") && identical(varName, "group")) next
6163
# add a line break if hovertext already exists
6264
if ("hovertext" %in% names(x)) x$hovertext <- paste0(x$hovertext, br())
6365
# text aestheic should be taken verbatim (for custom tooltips)

tests/testthat/test-ggplot-tooltip.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,19 @@ test_that("Hoverinfo is only displayed if no tooltip variables are present", {
119119
expect_true(all(grepl("^label", L$data[[2]]$text)))
120120
})
121121

122+
test_that("variable named 'group' is shown in tooltip when mapped to aesthetic (#2415)", {
123+
# When a user has a column named "group" and maps it to colour,
124+
# it should appear in the tooltip
125+
df <- data.frame(
126+
x = 1:6,
127+
y = 1:6,
128+
group = rep(c("A", "B"), each = 3)
129+
)
130+
p <- ggplot(df, aes(x, y, colour = group)) + geom_point()
131+
L <- plotly_build(ggplotly(p))$x
132+
# Check that "group: A" or "group: B" appears in the tooltip text
133+
txt <- unlist(lapply(L$data, function(d) d$text))
134+
expect_true(any(grepl("group: A", txt)))
135+
expect_true(any(grepl("group: B", txt)))
136+
})
137+

0 commit comments

Comments
 (0)