Skip to content
Open
Show file tree
Hide file tree
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
27 changes: 27 additions & 0 deletions R/plotly_build.R
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ plotly_build.plotly <- function(p, registerFrames = TRUE) {
}

if (NROW(builtData) > 0) {
alignMarker <- is_tidy(trace) && !has_group(trace) && is.list(trace[["marker"]])
if (alignMarker) builtData[[".plotlyRowIndex"]] <- seq_len(NROW(builtData))
# Build the index used to split one "trace" into multiple traces
isAsIs <- vapply(builtData, function(x) inherits(x, "AsIs"), logical(1))
isDiscrete <- vapply(builtData, is.discrete, logical(1))
Expand Down Expand Up @@ -254,6 +256,16 @@ plotly_build.plotly <- function(p, registerFrames = TRUE) {
c(".plotlyTraceIndex", ".plotlyGroupIndex",
if (inherits(trace, "plotly_line")) "x")
)
if (alignMarker) {
# Explicit marker arrays describe the same observations as x and y.
rows <- builtData[[".plotlyRowIndex"]]
if (!identical(rows, seq_along(isComplete))) {
trace[["marker"]] <- subset_point_attributes(
trace[["marker"]], Attrs[["marker"]], rows, length(isComplete)
)
}
builtData[[".plotlyRowIndex"]] <- NULL
}
builtData <- train_data(builtData, trace)
trace[[".plotlyVariableMapping"]] <- names(builtData)
# copy over to the trace data
Expand Down Expand Up @@ -1026,6 +1038,21 @@ supplyUserPalette <- function(default, user) {

# helper functions
array_ok <- function(attr) isTRUE(tryNULL(attr$arrayOk))

subset_point_attributes <- function(attrs, schema, rows, n) {
for (name in intersect(names(attrs), names(schema))) {
value <- attrs[[name]]
spec <- schema[[name]]
if (array_ok(spec) && is.atomic(value) && is.null(dim(value)) &&
length(value) == n) {
attrs[[name]] <- value[rows]
} else if (is.list(value) && is.list(spec)) {
attrs[[name]] <- subset_point_attributes(value, spec, rows, n)
}
}
attrs
}

has_fill <- function(trace) {
trace_type <- trace[["type"]] %||% "scatter"
# if trace type has fillcolor, but no fill attribute, then fill is always relevant
Expand Down
36 changes: 36 additions & 0 deletions tests/testthat/test-plotly-marker-alignment.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
test_that("marker arrays follow retained point coordinates", {
colorscale <- list(list(0, "blue"), list(0.5, "purple"), list(1, "green"))
p <- plot_ly() %>% add_trace(
type = "scatter", mode = "markers",
x = c(NA, 2, 3, 4, 5), y = 0,
marker = list(
color = c(0, 0, 0.5, 1, 1),
size = 5:9,
cmin = 0, cmax = 1, colorscale = colorscale,
line = list(color = c("red", "blue", "green", "black", "purple"), width = 2)
)
)

expect_warning(built <- plotly_build(p), "Ignoring 1 observations")
trace <- built$x$data[[1]]
expect_equal(as.numeric(trace$x), 2:5)
expect_equal(as.numeric(trace$marker$color), c(0, 0.5, 1, 1))
expect_equal(as.numeric(trace$marker$size), 6:9)
expect_equal(as.character(trace$marker$line$color), c("blue", "green", "black", "purple"))
expect_equal(trace$marker$line$width, 2)
expect_equal(trace$marker$colorscale, colorscale)
})

test_that("marker arrays stay aligned when observations are split into traces", {
p <- plot_ly(
x = c(10, 20, NA, 40, 50), y = 0, split = c("b", "a", "b", "a", "b"),
type = "scattergl", mode = "markers", marker = list(size = c(1, 2, 3, 4, 5))
)

expect_warning(built <- plotly_build(p), "Ignoring 1 observations")
traces <- setNames(built$x$data, vapply(built$x$data, `[[`, "", "name"))
expect_equal(as.numeric(traces$a$x), c(20, 40))
expect_equal(as.numeric(traces$a$marker$size), c(2, 4))
expect_equal(as.numeric(traces$b$x), c(10, 50))
expect_equal(as.numeric(traces$b$marker$size), c(1, 5))
})