Skip to content

Commit c066ffd

Browse files
committed
verify_guides() should just populate sensible defaults, not override user specified values; fixes #1137; fixes #1112
1 parent 7ee6c75 commit c066ffd

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

R/utils.R

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -822,10 +822,13 @@ verify_guides <- function(p) {
822822

823823
idx <- which(isBar)
824824
for (i in seq_along(idx)) {
825-
p <- colorbar_built(
826-
p, which = i, len = 1 / nGuides, y = 1 - ((i - 1) / nGuides),
827-
lenmode = "fraction", yanchor = "top"
828-
)
825+
# TODO: account for marker.line.colorbar
826+
j <- idx[[i]]
827+
bar <- p$x$data[[j]]$marker$colorbar
828+
p$x$data[[j]]$marker$colorbar$len <- bar$len %||% (1 / nGuides)
829+
p$x$data[[j]]$marker$colorbar$lenmode <- bar$lenmode %||% "fraction"
830+
p$x$data[[j]]$marker$colorbar$y <- bar$y %||% (1 - ((i - 1) / nGuides))
831+
p$x$data[[j]]$marker$colorbar$yanchor <- bar$yanchor %||% "top"
829832
}
830833

831834
}

tests/testthat/test-plotly-colorbar.R

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ expect_traces <- function(p, n.traces, name){
1010

1111
test_that("Can set colorbar attributes", {
1212
p <- plot_ly(mtcars, x = ~wt, y = ~cyl, color = ~cyl)
13-
p <- colorbar(p, len = 0.5)
13+
p <- colorbar(p, len = 0.3)
1414
l <- expect_traces(p, 2, "colorbar")
15-
expect_equivalent(l$data[[2]]$marker$colorbar$len, 0.5)
15+
expect_equivalent(l$data[[2]]$marker$colorbar$len, 0.3)
1616
})
1717

1818

@@ -43,6 +43,8 @@ test_that("Can expand z limits", {
4343
expect_equivalent(l$data[[1]]$zmax, 300)
4444
})
4545

46+
47+
# TODO: values outside the scale limits should probably be non-transparent (e.g. gray)
4648
test_that("Can restrict z limits", {
4749
p <- plot_ly(z = ~volcano)
4850
p <- colorbar(p, limits = c(140, 160))
@@ -54,3 +56,40 @@ test_that("Can restrict z limits", {
5456
dim(v) <- dim(volcano)
5557
expect_equivalent(l$data[[1]][["z"]], v)
5658
})
59+
60+
61+
test_that("colorbar does not affect mode of other traces", {
62+
# https://github.com/ropensci/plotly/issues/1196
63+
p <- plot_ly() %>%
64+
add_markers(data = mtcars, x= ~ hp, y= ~mpg, color = ~wt) %>%
65+
add_lines(x = seq(100, 300, length.out = 20), y = seq(10, 30, length.out = 20),
66+
color = I("black"))
67+
68+
expect_true(hide_colorbar(p)$x$data[[1]]$mode == "markers")
69+
expect_true(hide_colorbar(p)$x$data[[2]]$mode == "lines")
70+
expect_true(colorbar(p, limits = c(1,10))$x$data[[1]]$mode == "markers")
71+
expect_true(colorbar(p, limits = c(1,10))$x$data[[2]]$mode == "lines")
72+
73+
})
74+
75+
76+
test_that("can control both fill and stroke colorbars", {
77+
78+
p <- plot_ly(mtcars, x = ~wt, y = ~cyl, color = ~cyl, stroke = ~wt) %>%
79+
colorbar(title = "fill color", len = 0.4) %>%
80+
colorbar(title = "stroke color", len = 0.6, y = 0.55, which = 2)
81+
82+
d <- p$x$data
83+
expect_length(d, 3)
84+
85+
bar_fill <- d[[2]]$marker$colorbar
86+
expect_true(bar_fill$len == 0.4)
87+
expect_true(bar_fill$y == 1)
88+
expect_true(bar_fill$title == "fill color")
89+
90+
bar_stroke <- d[[3]]$marker$colorbar
91+
expect_true(bar_stroke$len == 0.6)
92+
expect_true(bar_stroke$y == 0.55)
93+
expect_true(bar_stroke$title == "stroke color")
94+
95+
})

0 commit comments

Comments
 (0)