Skip to content

Commit cae36fa

Browse files
committed
sf scattermapbox demos
1 parent 03b67ca commit cae36fa

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

demo/sf-data-scattermapbox.R

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
library(sf)
2+
library(plotly)
3+
4+
nc <- st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
5+
bb <- st_bbox(nc)
6+
7+
# TODO: make this more obvious and automatic
8+
plot_mapbox(
9+
data = group2NA(nc),
10+
x = ~x,
11+
y = ~y,
12+
mode = "lines",
13+
fillcolor = "green",
14+
fill = "toself",
15+
line = list(size = 0.01, color = "black")
16+
) %>%
17+
layout(
18+
mapbox = list(
19+
center = list(
20+
lat = mean(bb[c("ymin", "ymax")]),
21+
lon = mean(bb[c("xmin", "xmax")])
22+
),
23+
zoom = 6
24+
)
25+
)

demo/sf-scattermapbox.R renamed to demo/sf-layout-scattermapbox.R

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
library(albersusa)
21
library(sf)
3-
library(dplyr)
42
library(plotly)
53

6-
d <- counties_sf() %>%
7-
st_transform('+proj=longlat +datum=WGS84') %>%
8-
filter(!state %in% c("Alaska", "Hawaii"))
4+
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
95

106
# TODO: is there a way to do this without writing to disk?
117
sf_to_geojson <- function(x) {
@@ -14,22 +10,24 @@ sf_to_geojson <- function(x) {
1410
geojsonio::geojson_read(tmp, "local")
1511
}
1612

17-
plot_mapbox(lat = 45.5017, lon = -73.5673) %>%
13+
# By converting sf to geojson and routing to mapbox.layers, rendering
14+
# should be more performant (and correct in all cases). However, compared to
15+
# `demo("sf-data-scattermapbox.R", package = "plotly")`, you lose the ability
16+
# to interact with (and link) the features.
17+
plot_mapbox(x = -80, y = 35) %>%
1818
layout(
19-
height = 600,
20-
autosize = TRUE,
2119
hovermode = 'closest',
2220
mapbox = list(
2321
layers = list(
2422
list(
2523
sourcetype = 'geojson',
26-
source = sf_to_geojson(d),
24+
source = sf_to_geojson(nc),
2725
type = 'fill',
2826
color = 'transparent'
2927
)
3028
),
3129
bearing = 0,
32-
center = list(lat = 27.8, lon = -83),
30+
center = list(lat = 35, lon = -80),
3331
pitch = 0,
3432
zoom = 5.2,
3533
style = 'light'

0 commit comments

Comments
 (0)