forked from mikejohnson51/leaflet-intro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhospitals.Rmd
221 lines (182 loc) · 8.31 KB
/
hospitals.Rmd
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
---
title: "Example - Intro to Leaflet"
subtitle: "Exploring Seismic Vulnerability of Hospitals in Bogota, Colombia "
author: "Marcela Suarez"
output:
html_document:
toc: true
toc_float: true
toc_collapsed: true
---
```{r setup, include=FALSE, echo=FALSE}
# knitr::opts_chunk$set(echo=TRUE, warning=FALSE, message = FALSE)
knitr::opts_chunk$set(message = FALSE, warning = FALSE, comment = "#>", out.width = "100%")
```
****
This is a supplementary example for the leaflet-intro session.
****
# Basic Map
Load libraries
```{r load_libraries}
library(dplyr)
library(leaflet)
library(sf)
# library(htmlwidgets)
```
Load point data
```{r read_data}
hospitals_bogota <- readRDS(file = "data/hospitals_bogota.rds")
```
Data wrangling - point data
```{r data_wrangling}
hospitals_bogota <- hospitals_bogota %>%
mutate(lat = as.numeric(Latitude), long = as.numeric(Longitude)) %>%
select(-c(CITY, pais, SurgRms, IntensRms, EmergRms, CTRYISOA3, ADM1ISOC, ADM1ISON, SubRegion, PRIORITY, Map, Latitude, Longitude)) %>%
st_as_sf(coords = c("long", "lat"), crs = 4326)
```
Mapping hospital's location (as points)
```{r mapping_hospitals}
# Create a palette that maps factor levels to colors
pal <- colorFactor(c("#d8b365", "#5ab4ac"),
domain = c("Public - Ministry of Health ", "Private"))
leaflet(data = hospitals_bogota) %>%
addProviderTiles(providers$CartoDB) %>%
addCircleMarkers(radius = 8,
color = ~pal(H_Sector),
fillOpacity = 0.9,
stroke = FALSE,
label = ~Hospital_name,
popup = paste0("Surgery Rooms:", hospitals_bogota$SurgeryRm)) %>%
addLegend(pal = pal,
values = ~H_Sector,
opacity = 0.9,
title = "Hospital Sector")
```
Now same map but considering other attributes and visual variables.
```{r edit_hospitals_map}
pal <- colorFactor(c("#d8b365", "#5ab4ac"),
domain = c("Public - Ministry of Health ", "Private"))
addLegendCustom <- function(map, color, labels, sizes, opacity = 0.8, stroke, title = "Hospital Level"){
colorAdditions <- paste0(color, "; border-radius: 50%; width:", sizes, "px; height:", sizes, "px")
labelAdditions <- paste0("<div style='display: inline-block;height: ",
sizes, "px;margin-top: 4px;line-height: ", sizes, "px;'>",
labels, "</div>")
return(addLegend(map,
colors = colorAdditions,
labels = labelAdditions,
opacity = opacity,
title = title))
}
leaflet(data = hospitals_bogota) %>%
addProviderTiles(providers$CartoDB) %>%
addCircleMarkers(radius = ~ifelse(H_Level == "SMALL - General surgery and open 24 hrs", 4,
ifelse(H_Level == "MEDIUM -Two Specialty services", 8, 15)),
color = ~pal(H_Sector),
fillOpacity = 0.9,
stroke = FALSE,
label = ~Hospital_name,
popup = paste0("Surgery Rooms:", hospitals_bogota$SurgeryRm)) %>%
addLegendCustom(color = "gray",
labels = c("Small - General surgery and open 24 hrs",
"Medium -Two Specialty services",
"Big - High medical specialties"),
sizes = c(8, 15, 25),
stroke = FALSE) %>%
addLegend(pal = pal,
values = ~H_Sector,
opacity = 0.9,
title = "Hospital Sector")
```
# Adding Polygons
Load seismis hazard information (polygons representing hazard areas)
```{r read_hazard_info}
seismic_hazard <- read_sf("https://datosabiertos.bogota.gov.co/dataset/b3d097e8-0d97-44f5-90d7-e7d73530c33f/resource/62cc3e17-a69a-45de-a1cb-4ade5867c1ed/download/respuesta-sismica.geojson") %>%
st_transform(4326) # From EPSG:3857 (a Spherical Mercator projection coordinate system) to WGS84
```
Reclassify hazard areas
```{r reclass_hazard}
seismic_hazard <- seismic_hazard %>%
select(OBJECTID, RESSIS) %>%
mutate(reclass = ifelse(RESSIS == "Aluvial 50" | RESSIS == "Aluvial 100" | RESSIS == "Aluvial 200" | RESSIS == "Aluvial 300", "Aluvial",
ifelse(RESSIS == "Piedemonte A" | RESSIS == "Piedemonte B" | RESSIS == "Piedemonte C", "Piedemonte",
ifelse(RESSIS == "Lacustre Aluvial 200" | RESSIS == "Lacustre Aluvial 300", "Lacustre Aluvial",
ifelse(RESSIS == "Depósito Ladera", "Deposito Ladera",
ifelse(RESSIS == "Cerros", "Cerros", "Lacustre"))))))
```
**Put hospital's data together with seismic hazard information**
```{r final_map}
# Hospitals legend
pal <- colorFactor(c("#d8b365", "#5ab4ac"),
domain = c("Public - Ministry of Health ", "Private"))
addLegendCustom <- function(map, color, labels, sizes, opacity = 0.8, stroke, title = "Hospital Level"){
colorAdditions <- paste0(color, "; border-radius: 50%; width:", sizes, "px; height:", sizes, "px")
labelAdditions <- paste0("<div style='display: inline-block;height: ",
sizes, "px;margin-top: 4px;line-height: ", sizes, "px;'>",
labels, "</div>")
return(addLegend(map, colors = colorAdditions,
labels = labelAdditions, opacity = opacity, title = title))
}
# Hazard legend
pal_hazard <- colorFactor(c("#8c510a", "#1b7837", "#e08214", "#d73027", "#4575b4", "#fed976"),
domain = c("Aluvial", "Lacustre Aluvial", "Cerros", "Deposito Ladera", "Piedemonte", "Lacustre"))
#"#8c510a-brown", "#1b7837-green", "#4575b4-blue", "#e08214-orange", "#fed976-yellow", "#d73027-red"
map <- leaflet(data = hospitals_bogota) %>%
addProviderTiles(providers$CartoDB) %>%
addPolygons(data = seismic_hazard ,
stroke = FALSE,
fillOpacity = 0.5,
fillColor = ~pal_hazard(reclass),
label = ~reclass) %>%
addCircleMarkers(radius = ~ifelse(H_Level == "SMALL - General surgery and open 24 hrs", 4,
ifelse(H_Level == "MEDIUM -Two Specialty services", 8, 15)),
color = ~pal(H_Sector),
fillOpacity = 0.9,
stroke = FALSE,
label = ~Hospital_name,
popup = paste0("Surgery Rooms:", hospitals_bogota$SurgeryRm)) %>%
addLegendCustom(color = "gray",
labels = c("Small - General surgery and open 24 hrs",
"Medium -Two Specialty services",
"Big - High medical specialties"),
sizes = c(8, 15, 25),
stroke = FALSE) %>%
addLegend(pal = pal,
values = ~H_Sector,
opacity = 0.9,
title = "Hospital Sector") %>%
addLegend(pal = pal_hazard,
values = ~seismic_hazard$reclass,
title = "Seismic Hazard",
position = "bottomright")
# saveWidget(map, file="Map_Hospitals_Bogota.html")
map
```
# Creating a Map Function
Prepare a map function to be used in an upcoming shiny app
```{r function_izing}
hospital_map = function(hospital_level){
pal <- colorFactor(c("#d8b365", "#5ab4ac"),
domain = c("Public - Ministry of Health ", "Private"))
pal_hazard <- colorFactor(c("#8c510a", "#1b7837", "#e08214", "#d73027", "#4575b4", "#fed976"),
domain = c("Aluvial", "Lacustre Aluvial", "Cerros", "Deposito Ladera", "Piedemonte", "Lacustre"))
leaflet(data = hospitals_bogota %>%
filter(H_Level == hospital_level)) %>%
addProviderTiles(providers$CartoDB.DarkMatter) %>%
addPolygons(data = seismic_hazard ,
stroke = FALSE,
fillOpacity = 0.5,
fillColor = ~pal_hazard(reclass),
label = ~reclass) %>%
addCircleMarkers(color = ~pal(H_Sector),
fillOpacity = 0.9,
stroke = FALSE,
label = ~Hospital_name,
popup = paste0("Surgery Rooms:", hospitals_bogota$SurgeryRm))%>%
addLegend(pal = pal_hazard,
values = ~seismic_hazard$reclass,
title = "Seismic Hazard") %>%
addLegend(pal = pal,
values = ~H_Sector,
title = "Hospital Sector")
}
```