diff --git a/R/plotly_build.R b/R/plotly_build.R index f4a1c443b6..2d1fc0aa1a 100644 --- a/R/plotly_build.R +++ b/R/plotly_build.R @@ -131,6 +131,12 @@ plotly_build.plotly <- function(p, registerFrames = TRUE) { class = oldClass(x) ) + # determine trace type (if not specified, can depend on the # of data points) + # note that this should also determine a sensible mode, if appropriate + trace <- verify_type(trace) + # verify orientation of boxes/bars + trace <- verify_orientation(trace) + # attach crosstalk info, if necessary if (crosstalk_key() %in% names(dat) && isTRUE(trace[["inherit"]] %||% TRUE)) { trace[["key"]] <- trace[["key"]] %||% dat[[crosstalk_key()]] @@ -138,21 +144,11 @@ plotly_build.plotly <- function(p, registerFrames = TRUE) { } # if appropriate, tack on a group index - grps <- tryCatch( - as.character(dplyr::groups(dat)), - error = function(e) character(0) - ) - + grps <- if (has_group(trace)) tryNULL(dplyr::group_vars(dat)) if (length(grps) && any(lengths(trace) == NROW(dat))) { trace[[".plotlyGroupIndex"]] <- interaction(dat[, grps, drop = F]) } - # determine trace type (if not specified, can depend on the # of data points) - # note that this should also determine a sensible mode, if appropriate - trace <- verify_type(trace) - # verify orientation of boxes/bars - trace <- verify_orientation(trace) - # add sensible axis names to layout for (i in c("x", "y", "z")) { nm <- paste0(i, "axis") diff --git a/tests/figs/plotly-group/plotly-no-nas-for-irrelevant-group.svg b/tests/figs/plotly-group/plotly-no-nas-for-irrelevant-group.svg index bca4f00222..0f2b173918 100644 --- a/tests/figs/plotly-group/plotly-no-nas-for-irrelevant-group.svg +++ b/tests/figs/plotly-group/plotly-no-nas-for-irrelevant-group.svg @@ -1 +1 @@ -2345101520253035wtmpg +2345101520253035wtmpg diff --git a/tests/figs/plotly-group/simple-scatter-marker-color-group.svg b/tests/figs/plotly-group/simple-scatter-marker-color-group.svg new file mode 100644 index 0000000000..b70df8aeee --- /dev/null +++ b/tests/figs/plotly-group/simple-scatter-marker-color-group.svg @@ -0,0 +1 @@ +12345678910111213141516171819202122232425262728293031321015202530351.522.533.544.555.5mpgwt diff --git a/tests/testthat/test-plotly-group.R b/tests/testthat/test-plotly-group.R index a1337b02f6..0abb6599c9 100644 --- a/tests/testthat/test-plotly-group.R +++ b/tests/testthat/test-plotly-group.R @@ -65,3 +65,22 @@ test_that("Groups are ignored if grouping is irrelevant for the geom", { expect_length(l$data[[1]][["x"]], 32) expect_length(l$data[[1]][["y"]], 32) }) + + +test_that("Ordering of marker.color should not change in a simple scatterplot", { + + # https://github.com/ropensci/plotly/issues/1351 + l <- mtcars %>% + mutate(id = seq_len(nrow(.))) %>% + group_by(cyl) %>% + plot_ly( + x = ~mpg, y = ~wt, text = ~id, + marker = list(color = ~ifelse(id == 1, "black", "red")), + mode = "marker+text", textposition = "right" + ) %>% + expect_doppelganger_built("simple-scatter-marker-color-group") + + expect_true( + all(l$data[[1]]$marker$color == c("black", rep("red", 31))) + ) +})