Skip to content

Commit ce62a31

Browse files
committed
colorbar() fix for 'z'; accept x/y attrs in plot_geo()/plot_mapbox()
1 parent 81d57e5 commit ce62a31

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

R/helpers.R

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,16 @@ colorbar <- function(p, ...) {
1616
if (sum(isBar) != 1) {
1717
stop("This function only works with one colorbar")
1818
}
19-
p$x$data[[which(isBar)]]$marker$colorbar <- modify_list(
20-
p$x$data[[which(isBar)]]$marker$colorbar, list(...)
21-
)
19+
tr <- p$x$data[[which(isBar)]]
20+
if (inherits(tr, "zcolor")) {
21+
p$x$data[[which(isBar)]][["colorbar"]] <- modify_list(
22+
tr[["colorbar"]], list(...)
23+
)
24+
} else {
25+
p$x$data[[which(isBar)]]$marker$colorbar <- modify_list(
26+
tr$marker$colorbar, list(...)
27+
)
28+
}
2229
p
2330
}
2431

R/plotly_build.R

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,12 @@ plotly_build.plotly <- function(p) {
7575

7676
# trace type checking and renaming for plot objects
7777
if (is_mapbox(p) || is_geo(p)) {
78+
p <- geo2cartesian(p)
7879
p$x$attrs <- lapply(p$x$attrs, function(tr) {
79-
tr[["x"]] <- tr[["x"]] %||% tr[["lat"]]
80-
tr[["y"]] <- tr[["y"]] %||% tr[["lon"]]
8180
if (!grepl("scatter|choropleth", tr[["type"]] %||% "scatter")) {
8281
stop("Cant add a '", tr[["type"]], "' trace to a map object", call. = FALSE)
8382
}
84-
if (is_mapbox(p)) {
85-
tr[["type"]] <- "scattermapbox"
86-
}
83+
if (is_mapbox(p)) tr[["type"]] <- "scattermapbox"
8784
if (is_geo(p)) {
8885
tr[["type"]] <- if (!is.null(tr[["z"]])) "choropleth" else "scattergeo"
8986
}
@@ -99,6 +96,7 @@ plotly_build.plotly <- function(p) {
9996
rapply(x, eval_attr, data = dat, how = "list"),
10097
class = oldClass(x)
10198
)
99+
102100
# if appropriate, tack on a group index
103101
grps <- tryCatch(
104102
as.character(dplyr::groups(dat)),
@@ -478,6 +476,7 @@ map_color <- function(traces, title = "", na.color = "transparent") {
478476
if (traces[[i]][["type"]] == "contour") {
479477
traces[[i]]$colorscale[, 2] <- strip_alpha(traces[[i]]$colorscale[, 2])
480478
}
479+
traces[[i]] <- structure(traces[[i]], class = c("plotly_colorbar", "zcolor"))
481480
next
482481
}
483482
colorObj$color <- color[[i]]

R/utils.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ mapbox_token <- function() {
9696
token
9797
}
9898

99+
# rename attrs (unevaluated arguments) from geo locations (lat/lon) to cartesian
100+
geo2cartesian <- function(p) {
101+
p$x$attrs <- lapply(p$x$attrs, function(tr) {
102+
tr[["x"]] <- tr[["x"]] %||% tr[["lat"]]
103+
tr[["y"]] <- tr[["y"]] %||% tr[["lon"]]
104+
tr
105+
})
106+
p
107+
}
108+
99109
is_subplot <- function(p) {
100110
isTRUE(p$x$subplot)
101111
}

0 commit comments

Comments
 (0)