Skip to content

Commit a55d071

Browse files
committed
implement limits argument in colorbar()
1 parent e11b89c commit a55d071

File tree

5 files changed

+133
-21
lines changed

5 files changed

+133
-21
lines changed

R/helpers.R

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,64 @@
33
#' @param p a plotly object
44
#' @param ... arguments are documented here
55
#' \url{https://plot.ly/r/reference/#scatter-marker-colorbar}.
6+
#' @param limits numeric vector of length 2. Set the extent of the colorbar scale.
67
#' @author Carson Sievert
78
#' @export
89
#' @examples
910
#'
10-
#' plot_ly(mpg, x = ~cty, y = ~hwy, color = ~cyl) %>%
11-
#' colorbar(len = 0.5)
11+
#' p <- plot_ly(mtcars, x = ~wt, y = ~mpg, color = ~cyl)
1212
#'
13-
colorbar <- function(p, ...) {
13+
#' # pass any colorbar attribute --
14+
#' # https://plot.ly/r/reference/#scatter-marker-colorbar
15+
#' colorbar(p, len = 0.5)
16+
#'
17+
#' # Expand the limits of the colorbar
18+
#' colorbar(p, limits = c(0, 20))
19+
#' # values outside the colorbar limits are considered "missing"
20+
#' colorbar(p, limits = c(5, 6))
21+
#'
22+
#' # also works on colorbars generated via a z value
23+
#' corr <- cor(diamonds[vapply(diamonds, is.numeric, logical(1))])
24+
#' plot_ly(x = rownames(corr), y = colnames(corr), z = corr) %>%
25+
#' add_heatmap() %>%
26+
#' colorbar(limits = c(-1, 1))
27+
28+
colorbar <- function(p, ..., limits = NULL) {
1429
p <- plotly_build(p)
1530
isBar <- vapply(p$x$data, is.colorbar, logical(1))
1631
if (sum(isBar) != 1) {
1732
stop("This function only works with one colorbar")
1833
}
1934
tr <- p$x$data[[which(isBar)]]
20-
if (inherits(tr, "zcolor")) {
35+
hasZcolor <- inherits(tr, "zcolor")
36+
37+
# retrain limits of the colorscale
38+
if (!is.null(limits)) {
39+
limits <- sort(limits)
40+
if (hasZcolor) {
41+
z <- p$x$data[[which(isBar)]][["z"]]
42+
if (!is.null(dz <- dim(z))) {
43+
z <- c(z)
44+
}
45+
z[z < limits[1] | limits[2] < z] <- NA
46+
if (!is.null(dz)) dim(z) <- dz
47+
p$x$data[[which(isBar)]]$z <- z
48+
p$x$data[[which(isBar)]]$zmin <- limits[1]
49+
p$x$data[[which(isBar)]]$zmax <- limits[2]
50+
} else {
51+
# since the colorscale is in a different trace, retrain all traces
52+
p$x$data <- lapply(p$x$data, function(x) {
53+
col <- x$marker[["color"]]
54+
x$marker[["color"]][col < limits[1] | limits[2] < col] <- NA
55+
x$marker[["cmin"]] <- limits[1]
56+
x$marker[["cmax"]] <- limits[2]
57+
x
58+
})
59+
}
60+
}
61+
62+
# pass along ... to the colorbar
63+
if (hasZcolor) {
2164
p$x$data[[which(isBar)]][["colorbar"]] <- modify_list(
2265
tr[["colorbar"]], list(...)
2366
)
@@ -49,9 +92,9 @@ hide_guides <- function(p) {
4992
#' @seealso \code{\link{hide_legend}()}
5093
#' @examples
5194
#'
52-
#' plot_ly(economics, x = ~date, y = ~unemploy / pop, color = ~pop) %>%
53-
#' add_markers() %>%
54-
#' hide_colorbar()
95+
#' p <- plot_ly(mtcars, x = ~wt, y = ~cyl, color = ~cyl)
96+
#' hide_colorbar(p)
97+
#'
5598
hide_colorbar <- function(p) {
5699
p <- plotly_build(p)
57100
for (i in seq_along(p$x$data)) {
@@ -70,12 +113,11 @@ hide_colorbar <- function(p) {
70113
#'
71114
#' @param p a plotly object.
72115
#' @export
73-
#' @seealso \code{\link{hide_legend}()}
116+
#' @seealso \code{\link{hide_colorbat}()}
74117
#' @examples
75118
#'
76-
#' plot_ly(economics, x = ~date, y = ~unemploy / pop, color = ~pop) %>%
77-
#' add_markers() %>%
78-
#' hide_colorbar()
119+
#' p <- plot_ly(mtcars, x = ~wt, y = ~cyl, color = ~factor(cyl))
120+
#' hide_legend(p)
79121

80122
hide_legend <- function(p) {
81123
p <- plotly_build(p)

man/colorbar.Rd

Lines changed: 18 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/hide_colorbar.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/hide_legend.Rd

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
context("colorbar")
2+
3+
expect_traces <- function(p, n.traces, name){
4+
stopifnot(is.numeric(n.traces))
5+
L <- save_outputs(p, paste0("plotly-", name))
6+
expect_equal(length(L$data), n.traces)
7+
L
8+
}
9+
10+
11+
test_that("Can set colorbar attributes", {
12+
p <- plot_ly(mtcars, x = ~wt, y = ~cyl, color = ~cyl)
13+
p <- colorbar(p, len = 0.5)
14+
l <- expect_traces(p, 2, "colorbar")
15+
expect_equal(l$data[[2]]$marker$colorbar$len, 0.5)
16+
})
17+
18+
19+
test_that("Can expand limits", {
20+
p <- plot_ly(mtcars, x = ~wt, y = ~cyl, color = ~cyl)
21+
p <- colorbar(p, limits = c(0, 20))
22+
l <- expect_traces(p, 2, "colorbar-expand")
23+
expect_equal(l$data[[1]]$marker$cmin, 0)
24+
expect_equal(l$data[[2]]$marker$cmin, 0)
25+
expect_equal(l$data[[1]]$marker$cmax, 20)
26+
expect_equal(l$data[[2]]$marker$cmax, 20)
27+
})
28+
29+
test_that("Can restrict limits", {
30+
p <- plot_ly(mtcars, x = ~wt, y = ~cyl, color = ~cyl)
31+
p <- colorbar(p, limits = c(5, 7))
32+
l <- expect_traces(p, 2, "colorbar-restrict")
33+
expect_equal(unique(l$data[[1]]$marker$color), c(6, NA))
34+
expect_equal(l$data[[2]]$marker$cmin, 5)
35+
expect_equal(l$data[[2]]$marker$cmax, 7)
36+
})
37+
38+
test_that("Can expand z limits", {
39+
p <- plot_ly(z = ~volcano)
40+
p <- colorbar(p, limits = c(0, 300))
41+
l <- expect_traces(p, 1, "colorbar-z-expand")
42+
expect_equal(l$data[[1]]$zmin, 0)
43+
expect_equal(l$data[[1]]$zmax, 300)
44+
})
45+
46+
test_that("Can restrict z limits", {
47+
p <- plot_ly(z = ~volcano)
48+
p <- colorbar(p, limits = c(140, 160))
49+
l <- expect_traces(p, 1, "colorbar-z-restrict")
50+
expect_equal(l$data[[1]]$zmin, 140)
51+
expect_equal(l$data[[1]]$zmax, 160)
52+
v <- c(volcano)
53+
v[v < 140 | 160 < v] <- NA
54+
dim(v) <- dim(volcano)
55+
expect_equal(l$data[[1]][["z"]], v)
56+
})

0 commit comments

Comments
 (0)