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+ # '
5598hide_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
80122hide_legend <- function (p ) {
81123 p <- plotly_build(p )
0 commit comments