-
Notifications
You must be signed in to change notification settings - Fork 0
/
leafletmap.R
229 lines (142 loc) · 6.68 KB
/
leafletmap.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# install.packages("leaflet") from CRAN
# install_github("rstudio/leaflet") the development version from Github
library(leaflet)
library(leaflet.extras)
library(leaflet.extras2)
library(leaflegend)
library(readr)
library(sf)
library(rgdal)
# Leaflet is one of the most popular open-source JavaScript libraries for interactive maps
# This R package makes it easy to integrate and control Leaflet maps in R.
# Map tiles
# Markers
# Polygons
# Lines
# Popups
# Embed maps in R Markdown documents and Shiny apps
# Create a map widget by calling leaflet()
# Add layers (i.e., features) to the map by using layer functions
# (e.g. add Tiles, add Markers, add Polygons) to modify the map widget.
# A map is organized in a number of smaller portions known as map tiles.
# These map tiles are transferred one by one to your device and combined
# there to form a complete and ready-to-use map. A map tile URL contains the information
# a map library needs to determine where to get required map tiles from
town <- st_read("shapefiles/shapefiles/Accessibility Points/All_new_points.shp")
admin1 <- st_read("shapefiles/shapefiles/admin/LGA.shp")
pal <- colorNumeric(
palette = "Blues",
domain = data$partners_2)
roadpal <- colorFactor(c("seagreen", "#58280F","red","orange"),
domain = roads$type)
roads <- st_read("shapefiles/shapefiles/Accessibility Roads/All_new_roads.shp") %>%
st_transform(CRS("+init=epsg:4326"))
data <- read.csv("shapefiles/shapefiles/partners/partners.csv") %>%
st_as_sf(
# Columns
coords = c("long", "lat"),
remove = FALSE,
# Projection
crs = 4326
)
iconn = makeIcon("GitHub-Mark-removebg-preview.png", iconWidth = 20, iconHeight = 20)
icon = makeIcon(ifelse(town$Status == "Accessible Town", "img/accesble.png",
ifelse(town$Status == "Inaccessible Town", "img/inace.png" ,
ifelse( town$Status == "Hard to Reach Town", "img/hard.png",
ifelse( town$Status == "Hard to reach town - UNHAS only","img/helipad_neww.png", " "
))) ), iconWidth = 15, iconHeight = 15 )
map <- leaflet() %>%
addProviderTiles(providers$Esri.WorldGrayCanvas, group = "Esri.WorldGrayCanvas") %>%
addProviderTiles(providers$OpenStreetMap, group = "OpenStreetMap") %>%
addMarkers(town, lng = town$lon, lat = town$lat, group = "Town", icon = icon) %>%
addPolygons( data = admin1,
fillColor = ~pal(data$partners_2),
weight = 1,
opacity = 9
) %>%
addLabelOnlyMarkers(lng = data$long, lat = data$lat, label = data$LGAs_ADM2_,
labelOptions = labelOptions(noHide = T, direction = 'center', textOnly = T)) %>%
addMarkers(data, lng = data$long, lat = data$lat, clusterOptions = TRUE, group = "Point", popup = paste0("<b>LGA: </b>", data$LGAs_ADM_1, "<br>", "<b>NGOs: </b>", data$partners_2 ), icon = iconn) %>%
addLayersControl(baseGroups = c("Esri.WorldGrayCanvas","OpenStreetMap"),
overlayGroups = c("Point", "Town") )%>%
hideGroup("Town")
map
leafImg2 <- c("img/accesble.png", "img/hard.png", "img/inace.png","img/helipad_neww.png")
val2 <- c('Accessible town', 'Hard-to-reach town', "Extremely Hard-to-reach town", "Hard-to-reach town UNHAS Only")
leaflet() %>%
# A map is organized in a number of smaller portions known as map tiles.
# https://wiki.open street map.org/wiki/Tile_servers
addTiles( urlTemplate = "https://cartodb-basemaps-{s}.global.ssl.fastly.net/dark_all/{z}/{x}/{y}.png") %>%
# The leaflet package comes with 100+ providers tiles
# The names pf these tiles are stored in a list named providers
addProviderTiles(providers$Esri.WorldGrayCanvas, group = "Esri.WorldGrayCanvas") %>%
addProviderTiles(providers$HERE.satelliteDay, group = "HERE.satelliteDay") %>%
# Add an inset mini map
addMiniMap(
position = "bottomright",
tiles = providers$OpenStreetMap,
toggleDisplay = TRUE,
minimized = FALSE
) %>%
# Add measurement tool
addMeasure(
position = "topleft",
primaryLengthUnit = "meters",
secondaryLengthUnit = "kilometers",
primaryAreaUnit = "sqmeters"
) %>%
# Add scale bar
addScaleBar(
position = "bottomleft",
options = scaleBarOptions(imperial = FALSE)) %>%
# library(leaflet.extra)
addDrawToolbar(
targetGroup = "draw",
editOptions = editToolbarOptions(
selectedPathOptions = selectedPathOptions()
)
) %>%
# styling
addStyleEditor(position = "topleft",
openOnLeafletDraw = TRUE) %>%
addMarkers(town, lng = town$lon, lat = town$lat, popup = paste0(town$Settlement, "<br>", town$Status), group = "Town", icon = icon) %>%
# library(leaflegend)
addLegendImage(images = leafImg2,
labels = val2,
width = 10,
height = 10,
labelStyle = "font-size: 14px; vertical-align: middle;",
group = "Town",
title = 'Town Accessibility - Source: NIF/INSO MAP ',
position = 'bottomleft') %>%
addPolylines(data = roads, color = roadpal(roads$type), weight = 3, group = "Road", popup = ~paste("Road Status:",roads$type )) %>%
addLegend(
title = "Road Accessibility - Source: NIF/INSO MAP ",
pal = roadpal,
opacity = 1,
values = roads$type,
group = "Road",
position = "bottomleft") %>%
addPolygons( data = admin1,
stroke = 1,
color = "white",
fillColor = "#000000",
weight = 1,
group = "Admin",
opacity = 9
) %>%
addLabelOnlyMarkers(lng = data$long, lat = data$lat, label = data$LGAs_ADM2_, group = "Label",
labelOptions = labelOptions(noHide = T, direction = 'center', textOnly = T)) %>%
addLayersControl(
baseGroups = c("Esri.WorldGrayCanvas", "HERE.satelliteDay"),
overlayGroups = c("Town", "Road", "Admin", "Label")
)
# Learning Objectives
# Learn how to make interactive maps using leaflet
# Learn how to host an interactive map on web and github
# convert a table into a spacial object using st_as_sf() from the sf package
# So the problem with using leaflet generally is that it doesn't scale to large data
# so we have leaf gl this use webgl and when you do that you can plot hundreds of thousands
# of points in few seconds
# for chloroplast you have to first create a color palette by mapping it to the domain of the counts
# |> or %>%