Skip to content

Commit 3217e3d

Browse files
committed
add a demo of routing sf to geojson layer
This approach should still be useful for strange corner-cases (i.e., rings, holes) where we can't quite completely replicate geom_sf(). Note, however, when using layers, you lose the ability to tie data semantices to each feature (and, thus, lose the ability to link multiple views)
1 parent 65f74a5 commit 3217e3d

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

demo/sf-scattermapbox.R

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
library(albersusa)
2+
library(sf)
3+
library(dplyr)
4+
library(plotly)
5+
6+
d <- counties_sf() %>%
7+
st_transform('+proj=longlat +datum=WGS84') %>%
8+
filter(!state %in% c("Alaska", "Hawaii"))
9+
10+
# TODO: is there a way to do this without writing to disk?
11+
sf_to_geojson <- function(x) {
12+
tmp <- tempfile(fileext = ".geojson")
13+
st_write(x, tmp, driver = "GEOJSON")
14+
geojsonio::geojson_read(tmp, "local")
15+
}
16+
17+
plot_mapbox(lat = 45.5017, lon = -73.5673) %>%
18+
layout(
19+
height = 600,
20+
autosize = TRUE,
21+
hovermode = 'closest',
22+
mapbox = list(
23+
layers = list(
24+
list(
25+
sourcetype = 'geojson',
26+
source = sf_to_geojson(d),
27+
type = 'fill',
28+
color = 'transparent'
29+
)
30+
),
31+
bearing = 0,
32+
center = list(lat = 27.8, lon = -83),
33+
pitch = 0,
34+
zoom = 5.2,
35+
style = 'light'
36+
)
37+
)

0 commit comments

Comments
 (0)