Skip to content

Commit ae99ee1

Browse files
committed
deprecate add_choropleth(); rename geo() -> plot_geo(); rename mapbox -> plot_mapbox()
1 parent 4e8aac0 commit ae99ee1

File tree

6 files changed

+58
-41
lines changed

6 files changed

+58
-41
lines changed

R/add.R

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,14 @@ add_trace <- function(p, ..., color, symbol, size, linetype,
8585
if (is_mapbox(p) || is_geo(p)) {
8686
attrs[["x"]] <- attrs[["x"]] %||% attrs[["lat"]]
8787
attrs[["y"]] <- attrs[["y"]] %||% attrs[["lon"]]
88-
if (!grepl("scatter", attrs[["type"]])) {
88+
if (!grepl("scatter|choropleth", attrs[["type"]] %||% "scatter")) {
8989
stop("Cant add a '", attrs[["type"]], "' trace to a map object", call. = FALSE)
9090
}
9191
if (is_mapbox(p)) {
9292
attrs[["type"]] <- "scattermapbox"
9393
}
9494
if (is_geo(p)) {
95-
attrs[["type"]] <- "scattergeo"
95+
attrs[["type"]] <- if (!is.null(attrs[["z"]])) "choropleth" else "scattergeo"
9696
}
9797
}
9898

@@ -528,7 +528,6 @@ add_mesh <- function(p, x = NULL, y = NULL, z = NULL, ...,
528528
#' @inheritParams add_trace
529529
#' @rdname add_trace
530530
#' @export
531-
#'
532531
add_scattergeo <- function(p, ...) {
533532
.Deprecated("geo")
534533
p
@@ -537,24 +536,10 @@ add_scattergeo <- function(p, ...) {
537536
#' @inheritParams add_trace
538537
#' @rdname add_trace
539538
#' @export
540-
#' @examples
541-
#' density <- state.x77[, "Population"] / state.x77[, "Area"]
542-
#' plot_ly(z = ~density) %>%
543-
#' add_choropleth(locations = state.abb, locationmode = 'USA-states') %>%
544-
#' layout(geo = list(scope = "usa"))
545539
add_choropleth <- function(p, z = NULL, geo = NULL, ...,
546540
data = NULL, inherit = TRUE) {
547-
if (inherit) {
548-
z <- z %||% p$x$attrs[[1]][["z"]]
549-
geo <- geo %||% p$x$attrs[[1]][["geo"]] %||% "geo"
550-
}
551-
if (is.null(z)) {
552-
stop("Must supply `z` attribute", call. = FALSE)
553-
}
554-
add_trace_classed(
555-
p, class = "plotly_choropleth", z = z, type = "choropleth", geo = geo,
556-
..., data = data, inherit = inherit
557-
)
541+
.Deprecated("geo")
542+
p
558543
}
559544

560545
# attach a class to a trace which informs data processing in plotly_build

R/plotly.R

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,14 @@
3434
#' @param width Width in pixels (optional, defaults to automatic sizing).
3535
#' @param height Height in pixels (optional, defaults to automatic sizing).
3636
#' @param source Only relevant for \link{event_data}.
37-
#' @seealso \code{\link{layout}()}, \code{\link{add_trace}()}, \code{\link{style}()}
3837
#' @author Carson Sievert
38+
#' @seealso \itemize{
39+
#' \item For initializing a plotly-geo object: \code{\link{plot_geo}()}.
40+
#' \item For initializing a plotly-mapbox object: \code{\link{plot_mapbox}()}.
41+
#' \item For translating a ggplot2 object to a plotly object: \code{\link{ggplotly}()}.
42+
#' \item For modifying any plotly object: \code{\link{layout}()}, \code{\link{add_trace}()}, \code{\link{style}()}
43+
#' \item
44+
#' }
3945
#' @export
4046
#' @examples
4147
#' \dontrun{
@@ -153,11 +159,14 @@ plot_ly <- function(data = data.frame(), ..., type = NULL,
153159
#' valid scattermapbox attributes - \url{https://plot.ly/r/reference/#scattermapbox}.
154160
#' Note that x/y can also be used in place of lat/lon.
155161
#' @export
162+
#' @author Carson Sievert
163+
#' @seealso \code{\link{plot_ly}()}, \code{\link{plot_geo}()}, \code{\link{ggplotly}()}
164+
#'
156165
#' @examples \dontrun{
157166
#'
158167
#' map_data("world", "canada") %>%
159168
#' group_by(group) %>%
160-
#' mapbox(x = ~lat, y = ~long) %>%
169+
#' plot_mapbox(x = ~lat, y = ~long) %>%
161170
#' add_polygons() %>%
162171
#' layout(
163172
#' mapbox = list(
@@ -166,11 +175,12 @@ plot_ly <- function(data = data.frame(), ..., type = NULL,
166175
#' )
167176
#' }
168177
#'
169-
mapbox <- function(data = data.frame(), ...) {
170-
p <- plot_ly(data, ...)
171-
p <- config(p, mapboxAccessToken = mapbox_token())
172-
p$x$mapType <- "mapbox"
173-
p
178+
plot_mapbox <- function(data = data.frame(), ...) {
179+
p <- config(plot_ly(data), mapboxAccessToken = mapbox_token())
180+
# not only do we use this for is_mapbox(), but also setting the layout attr
181+
# https://plot.ly/r/reference/#layout-mapbox
182+
p$x$layout$mapType <- "mapbox"
183+
add_trace(p, ...)
174184
}
175185

176186
#' Initiate a plotly-geo object
@@ -182,17 +192,21 @@ mapbox <- function(data = data.frame(), ...) {
182192
#'
183193
#' @param ... arguments passed along to \code{\link{plot_ly}()}.
184194
#' @export
195+
#' @author Carson Sievert
196+
#' @seealso \code{\link{plot_ly}()}, \code{\link{plot_mapbox}()}, \code{\link{ggplotly}()}
185197
#' @examples
186198
#'
187199
#' map_data("world", "canada") %>%
188200
#' group_by(group) %>%
189-
#' geo(x = ~lat, y = ~long) %>%
201+
#' plot_geo(x = ~lat, y = ~long) %>%
190202
#' add_polygons()
191203
#'
192-
geo <- function(data = data.frame(), ...) {
193-
p <- plot_ly(data, ...)
194-
p$x$mapType <- "geo"
195-
p
204+
plot_geo <- function(data = data.frame(), ...) {
205+
p <- plot_ly(data)
206+
# not only do we use this for is_geo(), but also setting the layout attr
207+
# https://plot.ly/r/reference/#layout-geo
208+
p$x$layout$mapType <- "geo"
209+
add_trace(p, ...)
196210
}
197211

198212

man/geo.Rd renamed to man/plot_geo.Rd

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

man/plot_ly.Rd

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

man/mapbox.Rd renamed to man/plot_mapbox.Rd

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

man/plotly_json.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)