Skip to content

Commit 94fa7da

Browse files
Merge pull request plotly#2034 from moutikabdessabour/ggplotly_color_aes_fix
`ggplotly` not recognizing `color` as aesthetic mapping
2 parents b137432 + 9bddfc2 commit 94fa7da

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

R/ggplotly.R

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ ggplotly.ggmatrix <- function(p = ggplot2::last_plot(), width = NULL,
8888
height = NULL, tooltip = "all", dynamicTicks = FALSE,
8989
layerData = 1, originalData = TRUE, source = "A", ...) {
9090
dots <- list(...)
91+
9192
# provide a sensible crosstalk if none is already provided (makes ggnostic() work at least)
9293
if (!crosstalk_key() %in% names(p$data)) {
9394
p$data[[crosstalk_key()]] <- p$data[[".rownames"]] %||% seq_len(nrow(p$data))

R/layers2traces.R

+3
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ layers2traces <- function(data, prestats_data, layout, p) {
2323
# turn symbol (e.g., ..count..) & call (e.g. calc(count)) mappings into text labels
2424
map <- ggfun("make_labels")(map)
2525

26+
2627
# filter tooltip aesthetics down to those specified in `tooltip` arg
2728
if (!identical(p$tooltip, "all")) {
29+
# rectify tooltips, ggplot automatically convert `color` => `colour`
30+
p$tooltip[p$tooltip == "color"] <- "colour"
2831
map <- map[names(map) %in% p$tooltip | map %in% p$tooltip]
2932
}
3033

tests/testthat/test-ggplot-color.R

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
df = data.frame(width = 1:3, height = 1:3, col = letters[1:3])
2+
test_that("ggplotly automatically converts `color` aes to `colour`", {
3+
p <- qplot(width, height,
4+
data = df, color = col)
5+
# color variable is not shown
6+
color <- plotly_build(ggplotly(p, tooltip = c("color")))
7+
# colour (with u!) variable is shown
8+
expect_identical(color$x$data, plotly_build(ggplotly(p, tooltip = c("colour")))$x$data)
9+
})
10+

0 commit comments

Comments
 (0)