Skip to content

Commit 270c134

Browse files
committed
retrain color defaults in subplot. fixes #724
1 parent a336ffa commit 270c134

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

R/plotly_build.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,8 @@ map_color <- function(traces, title = "", na.color = "transparent") {
437437
col <- color[[i]] %||% colorDefaults[[idx]]
438438
alpha <- traces[[i]]$alpha %||% 1
439439
rgb <- toRGB(col, alpha)
440+
# we need some way to identify pre-specified defaults in subplot to retrain them
441+
if (is.null(color[[i]])) attr(rgb, "defaultAlpha") <- alpha
440442
obj <- if (hasLine[[i]]) "line" else if (hasMarker[[i]]) "marker" else if (hasText[[i]]) "textfont"
441443
traces[[i]][[obj]] <- modify_list(list(color = rgb), traces[[i]][[obj]])
442444
traces[[i]][[obj]] <- modify_list(list(fillcolor = rgb), traces[[i]][[obj]])

R/subplots.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ subplot <- function(..., nrows = 1, widths = NULL, heights = NULL, margin = 0.02
223223
data = Reduce(c, traces),
224224
layout = Reduce(modify_list, c(xAxes, rev(yAxes)))
225225
)
226+
# retrain default coloring
227+
p$data <- retrain_color_defaults(p$data)
228+
226229
# reposition shapes and annotations
227230
annotations <- Map(reposition, annotations, split(domainInfo, seq_along(plots)))
228231
shapes <- Map(reposition, shapes, split(domainInfo, seq_along(plots)))
@@ -333,3 +336,19 @@ reposition <- function(obj, domains) {
333336
}
334337
obj
335338
}
339+
340+
341+
retrain_color_defaults <- function(traces) {
342+
colorDefaults <- traceColorDefaults()
343+
for (i in seq_along(traces)) {
344+
# https://github.com/plotly/plotly.js/blob/c83735/src/plots/plots.js#L58
345+
idx <- i %% length(colorDefaults) + i %/% length(colorDefaults)
346+
newDefault <- colorDefaults[[idx]]
347+
for (j in c("marker", "line", "text")) {
348+
alpha <- attr(traces[[i]][[j]][["color"]], "defaultAlpha")
349+
if (is.null(alpha)) next
350+
traces[[i]][[j]][["color"]] <- toRGB(colorDefaults[[idx]], alpha)
351+
}
352+
}
353+
traces
354+
}

0 commit comments

Comments
 (0)