Skip to content

Support factor variables for x and/or y when numeric variable is used to set color #2404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@
## Bug fixes

* Closed #2337: Creating a new `event_data()` handler no longer causes a spurious reactive update of existing `event_data()`s. (#2339)
* Closed #1883: Support factor variables for x and/or y when numeric variable is used to set color. (#1883)

# 4.10.4

13 changes: 10 additions & 3 deletions R/plotly_build.R
Original file line number Diff line number Diff line change
@@ -872,9 +872,14 @@ map_color <- function(traces, stroke = FALSE, title = "", colorway, na.color = "
# add an "empty" trace with the colorbar
colorObj$color <- rng
colorObj$showscale <- default(TRUE)
# extract range for numeric variables
xValues <- unlist(lapply(traces, "[[", "x"))
xValues <- if (is.numeric(xValues)) range(xValues, na.rm = TRUE) else xValues
yValues <- unlist(lapply(traces, "[[", "y"))
yValues <- if (is.numeric(yValues)) range(yValues, na.rm = TRUE) else yValues
colorBarTrace <- list(
x = range(unlist(lapply(traces, "[[", "x")), na.rm = TRUE),
y = range(unlist(lapply(traces, "[[", "y")), na.rm = TRUE),
x = xValues,
y = yValues,
type = if (any(types %in% glTypes())) "scattergl" else "scatter",
mode = "markers",
opacity = 0,
@@ -885,7 +890,9 @@ map_color <- function(traces, stroke = FALSE, title = "", colorway, na.color = "
# 3D needs a z property
if ("scatter3d" %in% types) {
colorBarTrace$type <- "scatter3d"
colorBarTrace$z <- range(unlist(lapply(traces, "[[", "z")), na.rm = TRUE)
zValues <- unlist(lapply(traces, "[[", "x"))
zValues <- if (is.numeric(zValues)) range(zValues, na.rm = TRUE) else zValues
colorBarTrace$z <- zValues
}
if (length(type <- intersect(c("scattergeo", "scattermapbox"), types))) {
colorBarTrace$type <- type
7 changes: 7 additions & 0 deletions tests/testthat/test-plotly-color.R
Original file line number Diff line number Diff line change
@@ -20,6 +20,13 @@ test_that("Mapping a factor variable to color works", {
expect_equivalent(length(cols), 3)
})

test_that("A scatterplot with a factor variable and discrete color works", {
d <- palmerpenguins::penguins
p <- plot_ly(d, x = ~bill_length_mm, y = ~sex, color = ~bill_depth_mm,
type = 'scatter', mode = 'markers')
l <- expect_traces(p, 2, "scatterplot-factor-continous-color")
})

test_that("Custom RColorBrewer pallette works for factor variable", {
d <- palmerpenguins::penguins %>%
filter(!is.na(bill_length_mm))