Skip to content

Commit a6ba02d

Browse files
cpsievertclaude
andcommitted
Fix #2466: scale_*_manual with unused aesthetics no longer errors
When a scale has aesthetics = c("colour", "fill") but the plot only uses "colour", the code would try to access the non-existent "fill" column and error with "undefined columns selected". The fix uses intersect() to only process aesthetics that actually exist in each layer's data. Note: Trace splitting with multi-aesthetic scales is a separate issue (#2467). Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent f136000 commit a6ba02d

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

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)