Skip to content

Commit d24adde

Browse files
committed
add geo argument to add_choropleth()
1 parent 4c19ad4 commit d24adde

File tree

5 files changed

+56
-5
lines changed

5 files changed

+56
-5
lines changed

R/add.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,15 +490,17 @@ add_scattergeo <- function(p, geo = NULL, ..., data = NULL, inherit = TRUE) {
490490
#' plot_ly(z = ~density) %>%
491491
#' add_choropleth(locations = state.abb, locationmode = 'USA-states') %>%
492492
#' layout(geo = list(scope = "usa"))
493-
add_choropleth <- function(p, z = NULL, ..., data = NULL, inherit = TRUE) {
493+
add_choropleth <- function(p, z = NULL, geo = NULL, ...,
494+
data = NULL, inherit = TRUE) {
494495
if (inherit) {
495496
z <- z %||% p$x$attrs[[1]][["z"]]
497+
geo <- geo %||% p$x$attrs[[1]][["geo"]] %||% "geo"
496498
}
497499
if (is.null(z)) {
498500
stop("Must supply `z` attribute", call. = FALSE)
499501
}
500502
add_trace_classed(
501-
p, class = "plotly_choropleth", z = z, type = "choropleth",
503+
p, class = "plotly_choropleth", z = z, type = "choropleth", geo = geo,
502504
..., data = data, inherit = inherit
503505
)
504506
}

R/helpers.R

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
1+
#' Modify the colorbar
2+
#'
3+
#' @param p a plotly object
4+
#' @param ... arguments are documented here
5+
#' \url{https://plot.ly/r/reference/#scatter-marker-colorbar}.
6+
#' @author Carson Sievert
7+
#' @export
8+
#' @examples
9+
#'
10+
#' plot_ly(mpg, x = ~cty, y = ~hwy, color = ~cyl) %>%
11+
#' colorbar(len = 0.5)
12+
#'
13+
colorbar <- function(p, ...) {
14+
p <- plotly_build(p)
15+
isBar <- vapply(p$x$data, is.colorbar, logical(1))
16+
if (sum(isBar) != 1) {
17+
stop("This function only works with one colorbar")
18+
}
19+
p$x$data[[which(isBar)]]$marker$colorbar <- modify_list(
20+
p$x$data[[which(isBar)]]$marker$colorbar, list(...)
21+
)
22+
p
23+
}
24+
25+
126
#' Hide guides (legends and colorbars)
227
#'
328
#' @param p a plotly object.

R/layout.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ layout.plotly <- function(p, ..., data = NULL) {
3232

3333
#' Add a range slider to the x-axis
3434
#'
35-
#' @param a plotly object.
35+
#' @param p plotly object.
3636
#' @param ... these arguments are documented here
3737
#' \url{https://plot.ly/r/reference/#layout-xaxis-rangeslider}
3838
#' @export

man/colorbar.Rd

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

man/rangeslider.Rd

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

0 commit comments

Comments
 (0)