Skip to content

Commit b5248d8

Browse files
committed
route sf::st_bbox to geo's lataxis.range/lonaxis.range
1 parent 5f16b88 commit b5248d8

File tree

7 files changed

+108
-18
lines changed

7 files changed

+108
-18
lines changed

R/utils.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ mapbox_token <- function() {
210210
}
211211

212212
mapbox_fit_bounds <- function(p) {
213+
# Route trace[i]._bbox info to layout.mapboxid._fitBounds
214+
# so that we have a sensible range for each mapbox subplot
213215
mapboxIDs <- grep("^mapbox", sapply(p$x$data, "[[", "subplot"), value = TRUE)
214216
for (id in mapboxIDs) {
215217
bboxes <- lapply(p$x$data, function(tr) if (identical(id, tr$subplot)) tr[["_bbox"]])
@@ -232,6 +234,22 @@ mapbox_fit_bounds <- function(p) {
232234
)
233235
)
234236
}
237+
238+
# Route trace[i]._bbox info to layout.geoid.lonaxis/layout.geoid.lataxis
239+
geoIDs <- grep("^geo", sapply(p$x$data, "[[", "geo"), value = TRUE)
240+
for (id in geoIDs) {
241+
bboxes <- lapply(p$x$data, function(tr) if (identical(id, tr$geo)) tr[["_bbox"]])
242+
if (sum(lengths(bboxes)) == 0) next
243+
p$x$layout[[id]]$lataxis$range <- grDevices::extendrange(c(
244+
min(unlist(lapply(bboxes, "[[", "ymin")), na.rm = TRUE),
245+
max(unlist(lapply(bboxes, "[[", "ymax")), na.rm = TRUE)
246+
), f = 0.01)
247+
p$x$layout[[id]]$lonaxis$range <- grDevices::extendrange(c(
248+
min(unlist(lapply(bboxes, "[[", "xmin")), na.rm = TRUE),
249+
max(unlist(lapply(bboxes, "[[", "xmax")), na.rm = TRUE)
250+
), f = 0.01)
251+
}
252+
235253
p
236254
}
237255

demo/00Index

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@ crosstalk-filter-lines Using crosstalk's filter_select() to filter
1212
crosstalk-filter-dynamic-axis Using crosstalk's filter_select() to dynamically change the y-axis
1313
rotate Using htmlwidgets::onRender() to rotate the camera of a 3D graph
1414
ternary A basic ternary plot
15-
sf-data-scattermapbox Mapping sf objects with mapbox
16-
sf-layout-scattermapbox Mapping sf objects with mapbox (as a layer)
15+
sf-mapbox-data Mapping sf objects with mapbox
16+
sf-mapbox-layout Mapping sf objects with mapbox (as a layer)
17+
sf-ggplot2 Mapping sf objects via ggplot2 and geom_sf()
18+
sf-geo Mapping sf objects with scattergeo

demo/sf-data-scattermapbox.R

Lines changed: 0 additions & 16 deletions
This file was deleted.

demo/sf-geo.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
library(sf)
2+
library(plotly)
3+
4+
nc <- st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
5+
storms <- st_read(system.file("shape/storms_xyz.shp", package = "sf"), quiet = TRUE)
6+
7+
# TODO: geometry should be added as a grouping variable?
8+
subplot(
9+
plot_geo(nc),
10+
plot_geo(storms)
11+
)
12+

demo/sf-ggplot2.R

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
library(plotly)
2+
library(sf)
3+
4+
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
5+
p <- ggplot(nc) + geom_sf(aes(fill = AREA))
6+
ggplotly(p)
7+
8+
# If not supplied, coord_sf() will take the CRS from the first layer
9+
# and automatically transform all other layers to use that CRS. This
10+
# ensures that all data will correctly line up
11+
nc_3857 <- sf::st_transform(nc, "+init=epsg:3857")
12+
p2 <- ggplot() +
13+
geom_sf(data = nc) +
14+
geom_sf(data = nc_3857, colour = "red", fill = NA)
15+
ggplotly(p2)
16+
17+
# Unfortunately if you plot other types of feature you'll need to use
18+
# show.legend to tell ggplot2 what type of legend to use
19+
nc_3857$mid <- sf::st_centroid(nc_3857$geometry)
20+
p3 <- ggplot(nc_3857) +
21+
geom_sf(colour = "white") +
22+
geom_sf(aes(geometry = mid, size = AREA), show.legend = "point")
23+
ggplotly(p3)
24+
25+
# You can also use layers with x and y aesthetics: these are
26+
# assumed to already be in the common CRS.
27+
p4 <- ggplot(nc) +
28+
geom_sf() +
29+
annotate("point", x = -80, y = 35, colour = "red", size = 4)
30+
ggplotly(p4)

demo/sf-mapbox-data.R

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
library(sf)
2+
library(plotly)
3+
4+
nc <- st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
5+
6+
# can set multiple bounding boxes and overwrite attributes
7+
subplot(
8+
plot_mapbox(nc),
9+
plot_mapbox(nc, fillcolor = "gray", line = list(size = 0.01, color = "black"))
10+
)
11+
12+
# can map custom hover text to each point
13+
plot_mapbox(nc, text = ~AREA, hoverinfo = "text")
14+
15+
# TODO:
16+
# (1) this should create multiple traces!!! :(
17+
# (2) perhaps the colorbar definition needs fixing?
18+
plot_mapbox(nc, color = ~AREA, text = ~AREA, hoverinfo = "text", hoveron = "fill") %>%
19+
plotly_json()
20+
21+
# TODO: animation
22+
23+
24+
25+
# TODO: perhaps during verification, if hoveron = 'fill' for a given trace,
26+
# we could check if text is unique or not...if it is, just take first element
27+
plot_mapbox(nc, split = ~AREA, text = ~NAME, hoveron = "fill")
28+
29+
# TODO: how to best control hoverinfo?
30+
31+
# add dropdown for changing baselayer
32+
# TODO: how to keep the bounding box fixed?
33+
styles <- schema(FALSE)$layout$layoutAttributes$mapbox$style$values
34+
style_buttons <- lapply(styles, function(s) {
35+
list(label = s, method = "relayout", args = list("mapbox.style", s))
36+
})
37+
38+
p1 %>%
39+
layout(
40+
title = "Changing the base layer",
41+
updatemenus = list(list(y = 0.8, buttons = style_buttons))
42+
)
43+
44+
File renamed without changes.

0 commit comments

Comments
 (0)