-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathr-leaflet.r
134 lines (85 loc) · 3.75 KB
/
r-leaflet.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
library(leaflet)
library(rgdal)
library(htmlwidgets)
library(ggmap)
# *** GEOCODING ***
ourCoords <- geocode("425 S University Ave, philadelphia, pa")
# *** BASIC LEAFLET EXAMPLE ***
mymap <- leaflet() %>%
addTiles() %>%
addMarkers(ourCoords$lon, ourCoods$lat, popup="<strong>**MUSA 620**</strong>") %>%
setView(ourCoords$lon, ourCoods$lat, zoom = 17)
mymap
# *** MAP TILES ***
# Many options: https://leaflet-extras.github.io/leaflet-providers/preview/
# Design your own: https://www.mapbox.com/help/define-mapbox-studio-classic/.
# Default map tiles
leaflet() %>%
addTiles() %>%
# Neutral, light -- good for data visualization
leaflet() %>%
addProviderTiles(providers$CartoDB.Positron)
# Neutral, dark -- good for data visualization
leaflet() %>%
addProviderTiles(providers$CartoDB.DarkMatter)
# Satellite imagery
leaflet() %>%
addProviderTiles(providers$Esri.WorldImagery)
# Earth at night
leaflet() %>%
addProviderTiles(providers$NASAGIBS.ViirsEarthAtNight2012)
# Map tiles can also be layered
leaflet() %>% addProviderTiles(providers$MtbMap) %>%
addProviderTiles(providers$Stamen.TonerLines, options = providerTileOptions(opacity = 0.35)) %>%
addProviderTiles(providers$Stamen.TonerLabels)
# *** POINT OVERLAYS ***
accidentpoints <- read.csv("d:/philly-accident-points.csv")
personBins <- c(0, 1, 2, 5, 10, 20, Inf)
paletteBins <- colorBin(palette = "OrRd", accidentpoints$persons, bins=-personBins)
paletteFactor <- colorFactor(c("blue","red","green"), accidentpoints$drunk_dr)
paletteContinuous <- colorNumeric(palette = "magma", domain = accidentpoints$latitude + accidentpoints$longitud)
leaflet() %>%
addProviderTiles(providers$CartoDB.DarkMatterNoLabels) %>%
#addProviderTiles(providers$CartoDB.PositronNoLabels) %>%
addCircleMarkers(data=accidentpoints,
lng = ~longitud,
lat = ~latitude,
radius = ~sqrt(persons) + 0.5,
fillOpacity = 1,
fillColor = ~paletteBins(-persons),
#fillColor = ~paletteFactor(drunk_dr),
#fillColor = ~paletteContinuous(latitude + longitud),
#color = "white",
#weight = 2,
#opacity = 1,
stroke=FALSE,
#popup= ~paste0("Date: ",month,"/",day),
label = ~tway_id)
# *** POLYGON OVERLAYS ***
phillycrime2016 <- readOGR("d:/philly-crime-2016.geojson", "OGRGeoJSON")
phillycrime2016$crimebucket <- factor(
cut(as.numeric(crimePlot2016$y2016), c(-1, 300, 700, 1200, 1801, 99999999)),
labels = c("Less than 300", "300 to 699", "700 to 1199", "1200 to 1800", "More than 1800")
)
crimeBucketPal <- colorFactor(c("#3b2a3d", "#755a4a", "#8b844a", "#e3c700", "#e2e900"), phillycrime2016$crimebucket)
crime2016 <- leaflet(phillycrime2016) %>% addProviderTiles(providers$CartoDB.Positron) %>%
addPolygons( data = phillycrime2016,
fillColor = ~crimeBucketPal(crimebucket),
weight = 0.8,
opacity = 0.9,
smoothFactor = 0.1,
color = ~crimeBucketPal(crimebucket),
fillOpacity = 0.9,
label = ~paste0("Crimes: ", y2016),
highlight = highlightOptions(
fillColor = "orange",
fillOpacity = 1,
bringToFront = FALSE)) %>%
addLegend(pal = crimeBucketPal,
values = ~crimebucket,
position = "bottomright",
title = "Crimes in 2016",
opacity = 1)
crime2016
# *** EXPORT TO THE WEB ***
saveWidget(crime2016, file="philly-crime.html", selfcontained=TRUE)