Skip to content

Commit d94b58e

Browse files
committed
retrain defaults in verify_guides() and account for zcolor; fixes #1235
1 parent 982836b commit d94b58e

File tree

2 files changed

+69
-10
lines changed

2 files changed

+69
-10
lines changed

R/utils.R

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -781,27 +781,39 @@ verify_guides <- function(p) {
781781
}
782782

783783
isVisibleBar <- function(tr) {
784-
is.colorbar(tr) && isTRUE(tr$showscale %||% TRUE)
784+
is.colorbar(tr) && (tr$showscale %||% TRUE)
785785
}
786786
isBar <- vapply(p$x$data, isVisibleBar, logical(1))
787787
nGuides <- sum(isBar) + has_legend(p)
788788

789789
if (nGuides > 1) {
790790

791791
# place legend at bottom since its scrolly
792-
p$x$layout$legend <- modify_list(
793-
list(y = 1 - ((nGuides - 1) / nGuides), yanchor = "top"),
794-
p$x$layout$legend
795-
)
792+
yanchor <- default("top")
793+
y <- default(1 - ((nGuides - 1) / nGuides))
794+
p$x$layout$legend$yanchor <- p$x$layout$legend$yanchor %|D|% yanchor
795+
p$x$layout$legend$y <- p$x$layout$legend[["y"]] %|D|% y
796796

797+
# shrink/position colorbars
797798
idx <- which(isBar)
798799
for (i in seq_along(idx)) {
800+
len <- default(1 / nGuides)
801+
lenmode <- default("fraction")
802+
y <- default(1 - ((i - 1) / nGuides))
803+
799804
j <- idx[[i]]
800-
bar <- p$x$data[[j]]$marker$colorbar
801-
p$x$data[[j]]$marker$colorbar$len <- bar$len %||% (1 / nGuides)
802-
p$x$data[[j]]$marker$colorbar$lenmode <- bar$lenmode %||% "fraction"
803-
p$x$data[[j]]$marker$colorbar$y <- bar$y %||% (1 - ((i - 1) / nGuides))
804-
p$x$data[[j]]$marker$colorbar$yanchor <- bar$yanchor %||% "top"
805+
tr <- p$x$data[[j]]
806+
if (inherits(tr, "zcolor")) {
807+
p$x$data[[j]]$colorbar$len <- tr$colorbar$len %|D|% len
808+
p$x$data[[j]]$colorbar$lenmode <- tr$colorbar$lenmode %|D|% "fraction"
809+
p$x$data[[j]]$colorbar$y <- tr$colorbar$y %|D|% y
810+
p$x$data[[j]]$colorbar$yanchor <- tr$colorbar$yanchor %|D|% yanchor
811+
} else {
812+
p$x$data[[j]]$marker$colorbar$len <- tr$marker$colorbar$len %|D|% len
813+
p$x$data[[j]]$marker$colorbar$lenmode <- tr$marker$colorbar$lenmode %|D|% "fraction"
814+
p$x$data[[j]]$marker$colorbar$y <- tr$marker$colorbar$y %|D|% y
815+
p$x$data[[j]]$marker$colorbar$yanchor <- tr$marker$colorbar$yanchor %|D|% yanchor
816+
}
805817
}
806818

807819
}

tests/testthat/test-plotly-colorbar.R

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,51 @@ test_that("can control both fill and stroke colorbars", {
9292
expect_true(bar_stroke$y == 0.55)
9393
expect_true(bar_stroke$title == "stroke color")
9494

95+
})
96+
97+
test_that("positioning with multiple colorbars and legends", {
98+
99+
s <- subplot(
100+
plot_ly(z = ~volcano),
101+
plot_ly(x = 1:10, y = 1:10, color = 1:10)
102+
)
103+
104+
b <- plotly_build(s)
105+
d <- b$x$data
106+
expect_length(d, 3)
107+
108+
expect_true(d[[1]]$colorbar$len == 1/3)
109+
expect_true(d[[1]]$colorbar$lenmode == "fraction")
110+
expect_true(d[[1]]$colorbar$yanchor == "top")
111+
expect_true(d[[1]]$colorbar$y == 1)
112+
113+
expect_true(d[[3]]$marker$colorbar$len == 1/3)
114+
expect_true(d[[3]]$marker$colorbar$lenmode == "fraction")
115+
expect_true(d[[3]]$marker$colorbar$yanchor == "top")
116+
expect_equal(as.numeric(d[[3]]$marker$colorbar$y), 2/3, tolerance = 0.01)
117+
118+
expect_true(b$x$layout$legend$yanchor == "top")
119+
expect_equal(as.numeric(b$x$layout$legend$y), 1/3, tolerance = 0.01)
120+
121+
122+
s <- subplot(
123+
plot_ly(z = ~volcano),
124+
plot_ly(x = 1:10, y = 1:10, color = factor(1:10))
125+
)
126+
127+
b <- plotly_build(s)
128+
d <- b$x$data
129+
expect_length(d, 11)
130+
131+
expect_true(d[[1]]$colorbar$len == 0.5)
132+
expect_true(d[[1]]$colorbar$lenmode == "fraction")
133+
expect_true(d[[1]]$colorbar$yanchor == "top")
134+
expect_true(d[[1]]$colorbar$y == 1)
135+
136+
expect_true(b$x$layout$legend$y == 0.5)
137+
expect_true(b$x$layout$legend$yanchor == "top")
138+
139+
140+
141+
95142
})

0 commit comments

Comments
 (0)