File tree 2 files changed +33
-10
lines changed
2 files changed +33
-10
lines changed Original file line number Diff line number Diff line change
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
+ )
Original file line number Diff line number Diff line change 1
- library(albersusa )
2
1
library(sf )
3
- library(dplyr )
4
2
library(plotly )
5
3
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 )
9
5
10
6
# TODO: is there a way to do this without writing to disk?
11
7
sf_to_geojson <- function (x ) {
@@ -14,22 +10,24 @@ sf_to_geojson <- function(x) {
14
10
geojsonio :: geojson_read(tmp , " local" )
15
11
}
16
12
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 ) %> %
18
18
layout(
19
- height = 600 ,
20
- autosize = TRUE ,
21
19
hovermode = ' closest' ,
22
20
mapbox = list (
23
21
layers = list (
24
22
list (
25
23
sourcetype = ' geojson' ,
26
- source = sf_to_geojson(d ),
24
+ source = sf_to_geojson(nc ),
27
25
type = ' fill' ,
28
26
color = ' transparent'
29
27
)
30
28
),
31
29
bearing = 0 ,
32
- center = list (lat = 27.8 , lon = - 83 ),
30
+ center = list (lat = 35 , lon = - 80 ),
33
31
pitch = 0 ,
34
32
zoom = 5.2 ,
35
33
style = ' light'
You can’t perform that action at this time.
0 commit comments