Skip to content

Commit 0f52660

Browse files
committed
Fix #2466: scale_*_manual with unused aesthetics no longer errors
1 parent 3292abd commit 0f52660

3 files changed

Lines changed: 19 additions & 4 deletions

File tree

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* `plotly_build()` now works with `ggmatrix` objects (e.g., from `GGally::ggpairs()`). (#2447)
1010
* Closed #2415: `ggplotly()` now shows variables named 'group' in tooltips when mapped to aesthetics like `colour`.
1111
* Closed #2455, #2460: `ggplotly()` no longer creates empty shapes when `panel.border` is `element_blank()` (ggplot2 4.0.0 compatibility).
12+
* Closed #2466: `ggplotly()` no longer errors when `scale_*_manual()` has unused aesthetics (e.g., `aesthetics = c("colour", "fill")` when only colour is used).
1213

1314
# plotly 4.11.0
1415

R/ggplotly.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,9 +411,10 @@ gg2list <- function(p, width = NULL, height = NULL,
411411
# of each non-positional scale for display in tooltips
412412
for (sc in npscales$scales) {
413413
data <- lapply(data, function(d) {
414-
# scale may not be relevant for every layer data
415-
if (any(names(d) %in% sc$aesthetics)) {
416-
d[paste0(sc$aesthetics, "_plotlyDomain")] <- d[sc$aesthetics]
414+
# Only process aesthetics that actually exist in this layer's data
415+
present_aes <- intersect(sc$aesthetics, names(d))
416+
if (length(present_aes) > 0) {
417+
d[paste0(present_aes, "_plotlyDomain")] <- d[present_aes]
417418
}
418419
d
419420
})

tests/testthat/test-ggplot-color.R

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
df = data.frame(width = 1:3, height = 1:3, col = letters[1:3])
22
test_that("ggplotly automatically converts `color` aes to `colour`", {
3-
p <- qplot(width, height,
3+
p <- qplot(width, height,
44
data = df, color = col)
55
# color variable is not shown
66
color <- plotly_build(ggplotly(p, tooltip = c("color")))
77
# colour (with u!) variable is shown
88
expect_identical(color$x$data, plotly_build(ggplotly(p, tooltip = c("colour")))$x$data)
99
})
1010

11+
test_that("scale_*_manual with unused aesthetics does not error (#2466)", {
12+
# scale has aesthetics = c('colour', 'fill') but plot only uses 'colour'
13+
p <- ggplot(iris, aes(Sepal.Length, Sepal.Width, colour = Species)) +
14+
geom_point() +
15+
scale_colour_manual(
16+
values = c("setosa" = "red", "versicolor" = "blue", "virginica" = "green"),
17+
aesthetics = c("colour", "fill")
18+
)
19+
# Should not error with "undefined columns selected"
20+
# (Note: trace splitting with multi-aesthetic scales is a separate issue #2467)
21+
expect_error(plotly_build(ggplotly(p)), NA)
22+
})
23+

0 commit comments

Comments
 (0)