-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtmap-toronto-corona.Rmd
More file actions
391 lines (229 loc) · 12.1 KB
/
Copy pathtmap-toronto-corona.Rmd
File metadata and controls
391 lines (229 loc) · 12.1 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
---
title: "Creating maps using tmap for Toronto Coronavirus Cases"
author: |
| Lauren Yee
| lauren@mapdatascience.com
| [www.mapdatascience.com](www.mapdatascience.com)
date: "28/10/2020"
output:
html_document:
toc: TRUE
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,fig.path = './Figures/', dev="png",dpi=300)
library(sf)
library(dplyr)
library(tmap)
library(readr)
library(stringr)
library(RColorBrewer)
library(viridis)
library(tidyr)
```
This document serves as a mini-tutorial on using tmap library for creating thematic maps in R.
We will be visualizing COVID-19 Cases in Toronto Neighbourhoods.
Disclaimer!!
This data is based solely on COVID-19 cases and does not consider, population, population density, multi-generational homes, long-term care, socioeconomics, access to care, unhoused populations or other factors that would impact the number of cases in a given neighbourhood.
Please note, this tutorial serves as a basis for quickly illustrating how tmap works for a Guest Lecture at University of Toronto. It is not an exhaustive resource for interpreting public health data, as COVID-19-19 is still ongoing the data presented in this tutorial is taken as a snapshot in time from the beginning of October 2020. This data is subject to change and may differ from official data sources, as they remove duplicates and resolve data issues. There is only so much that can be interpreted from maps or visualizations alone and subject matter experts (epidemiologists, public health researchers, virologists, etc.) should always be consulted when examining public health data - especially in this climate.
# Metadata
The metadata for COVID-19 cases by Toronto neighbourhood is available here:
https://open.toronto.ca/dataset/covid-19-cases-in-toronto/
**Toronto Neighbourhoods** were also downloaded from the Toronto Open data portal:
https://open.toronto.ca/dataset/neighbourhoods/
Please review the metadata page for explanations of column names and important characteristics of the data.
## Read in toronto neighbourhoods and covid data
```{r}
toronto <- st_read("./outputs/toronto_neighbourhoods.gpkg")
neighbourhood_covid<-st_read("./outputs/toronto_neighbourhoods_covid.gpkg")
covid<- read_csv('./Data/COVID19 cases.csv')
covid<-tibble(covid, .name_repair = make.names)
```
# Missing Data
We can calculate the total number of COVID-19 cases per neighbourhood:
```{r}
covid_counts <- covid %>%
group_by(Neighbourhood.Name)%>%
count()
covid %>%filter(is.na(Neighbourhood.Name))
```
There is missing neighbourhoods in some of the COVID-19 data (n=721). From Toronto's open data website for the COVID-19-19 cases, the metadata indicates that neighbourhood name is created by:
>Client postal code information is mapped to the most up-to-date census tract (CT) and neighbourhood information available from the city. As a result, neighbourhood information is not available for those with missing postal code or when postal code could not be mapped/linked to a CT.
Therefore, the missing data may be because of a missing postal code or for people who were tested in Toronto but live outside the city.
Let's remove them for now
```{r}
covid_counts<-covid_counts %>%filter(!is.na("Neighbourhood.Name"))
```
# Tmap
## Create map of total positive covid cases by neighbourhood
Now that our counts are joined to the spatial neighbourhoods of Toronto, we can begin to map them with Tmap.
>Like ggplot2, tmap is based on the idea of a ‘grammar of graphics’ (Wilkinson and Wills 2005). This involves a separation between the input data and the aesthetics (how data are visualised): each input dataset can be ‘mapped’ in a range of different ways including location on the map (defined by data’s geometry), color, and other visual variables. The basic building block is tm_shape() (which defines input data, raster and vector objects), followed by one or more layer elements such as tm_fill() and tm_borders()
- [Geocomputation in R](https://geocompr.robinlovelace.net/adv-map.html)
We want to map the shape of the neighbourhoods `neighbourhood_covid` and show the differences in the total number of cases `n` by using `tm_fill`. We can add a white border with a line width `lwd = 0.5` of 0.5.
```{r tmap}
tm_shape(neighbourhood_covid) +
tm_fill("n")+
tm_borders("white", lwd=0.5)
```
We can change the colour palette:
```{r tmap palette}
tm_shape(neighbourhood_covid) +
tm_fill("n",palette = brewer.pal("Greens", n = 6))+
tm_borders("black", lwd=0.5)
```
# Add Histogram to Map
```{r tmap histo}
tm_shape(neighbourhood_covid) +
tm_fill("n",palette = brewer.pal("Greens", n = 6),
legend.hist = T)+
tm_borders("black", lwd=0.5)+
tm_legend(outside = TRUE, hist.width=2)+
tm_layout(legend.hist.size = 0.5)
```
# Proportional Symbol
Use `tm_dots` for proportional symbols
```{r}
tm_shape(neighbourhood_covid)+
tm_dots(size="n")+
tm_borders("black", lwd=0.5)
```
# Add other map elements
scale bar : `tm_scale_bar`
compass : `tm_compass`
main title `main.title`
```{r tmap elements}
neighbourhood_covid<-st_transform(neighbourhood_covid,2958)
tm_shape(neighbourhood_covid) +
tm_fill("n",palette = brewer.pal("Greens", n = 6))+
tm_borders("black", lwd=0.5)+
tm_scale_bar(width = 0.22) +
tm_compass(position = c(0.85,0.4)) +
tm_layout(main.title = "Total Positive Covid Cases by Neighbourhood", bg.color = "lightgrey", inner.margins = c(0, 0, 0, 0),
frame.lwd = 5, title.size = 1)
```
# Subset Data to Display
If we wanted to show the neighbourhoods that have more than 500 cases:
First we can subset the data :
```{r}
over_500<-neighbourhood_covid %>%
filter(n>500)
```
First we want to show **ALL** the neighbourhoods like we have previously using `neighbourhood_covid` but this time we're going to set the fill for these to white and the border to grey, so that they take a backseat the neighbourhood we want to highlight.
Then we can take our new subset `over_500` and display this in blue on top of `neighbourhood_covid`. This is where the `layered grammar` allows us to build up our maps.
To label neighbourhood names `tm_text`
```{r tmap_subset}
tm_shape(neighbourhood_covid) +
tm_fill("white")+
tm_borders("grey", lwd=0.5)+
tm_shape(over_500)+
tm_fill("lightblue")+
tm_borders("black")+
tm_text("Neighbourhood.Name", just="left", xmod=0.5, size=0.8, bg.color = "white", remove.overlap = TRUE)+
tm_layout(main.title = "Top 5 Neighbourhoods for Covid Cases", inner.margins = c(0, 0, 0, 0))
```
# Add basemaps
Using library `rosm`.
`osm.raster` will download tiles directly to your computer (may take some time) based on the square boundary or bounding box around Toronto. Type `?osm.raster` into the console to see the options available. `Type` changes the type of basemap to download while crop, crops the basemap to the bbox extent.
`tm_rgb` will display the basemap raster.
````{r tmap basemap, message=FALSE, warning=FALSE}
library(rosm)
bg <- osm.raster(st_bbox(toronto),crop=TRUE)
tm_shape(bg)+
tm_rgb()+
tm_shape(toronto)+
tm_borders()
bg_hotstyle<-osm.raster(st_bbox(toronto), type="hotstyle")
tm_shape(bg_hotstyle)+
tm_rgb()+
tm_shape(toronto)+
tm_borders(col = "black")+
tm_style("classic")
```
# Create Custom Icons for Tmap
tmap contains two modes:
plot: static maps, shown in graphics device window; can be exported to png, jpg, pdf, etc.
`tmap_mode("plot")`
view: interactive maps, shown in the viewing window or in the browser; can be exported to standalone HTML files
`tmap_mode("view")`
To use our own symbols in tmap, we can use any photo you like. I searched the noun project for "corona" and downloaded that to the images/ folder.
https://thenounproject.com/search/?q=corona
Once you have an image, assign it to a variable `cv` and use `tmap_icons` to load the image.
This method requires an interactive map (not covered here) so make sure tmap is in view mode!
Using `tm_symbols` we assign our shape to our variable `cv` e.g. what shape do you want to symbolize your data?
We will then scale the size of the SARS-CoV-2 image to the number of positive cases.
```{r tmap icons}
cv<-tmap_icons('./Images/noun_virus_3364091.png')
current.mode <- tmap_mode("view")
tm_shape(neighbourhood_covid)+
tm_symbols(shape=cv, size = "n")
tmap_mode(current.mode)
```
**Note this graphic may not show correctly if viewing in html, try opening the actual .rmd provided to see how this works**
```{r png map}
knitr::include_graphics('images/corona_map.png')
```
## Quick multiples of maps
Within the covid data we can also look at what specific outcomes there are. The three options are active, fatal or resolved. The resolved cases are significantly higher than the other two options since our data looks over the entire time period from the start of the pandemic.
```{r outcomes}
outcomes <- covid %>%group_by(Neighbourhood.Name, Outcome)%>%
count()%>%
pivot_wider(names_from = "Outcome", values_from = "n", values_fill = 0)
toronto_outcomes <- toronto %>%
full_join(outcomes, by= "Neighbourhood.Name")
tm_shape(toronto_outcomes) +
tm_fill(c("ACTIVE","FATAL","RESOLVED"),palette = brewer.pal("Greens", n = 6))+
tm_borders("black", lwd=0.5)
```
We can quickly create multiple maps by this methodology - but look at the legends for each map.
Having three separate legends with different breaks can make it difficult to interpret across maps.
## Custom scale
Custom scales can be created for any map, but caution should be used to make sure you are not hiding or misrepresenting your data.
`outcome_breaks` was created with custom intervals and specified in `tm_fill` in the `breaks` argument.
Aesthetically we can also generate just ONE legend outside of both maps so it is easier to read.
I also removed `resolved` from being displayed as we are more interested in active and fatal cases:
```{r outcomes breaks}
toronto_outcomes<-toronto_outcomes %>% select(-RESOLVED)
outcome_breaks <- c(0,1,15,30,45,60,75,80)
tm_shape(toronto_outcomes) +
tm_fill(c("ACTIVE","FATAL"),breaks = outcome_breaks,title="Cases")+
tm_borders("black", lwd=0.5)+
tm_layout(panel.labels =c("Active Cases","Fatal Cases"),
legend.outside = TRUE
)
```
# Different Breaks
Custom breaks as we saw above can impact how data is displayed.
There are many breaks pre-set in `tmap` that you can use. Below is an example of a few that can drastically impact how alike or how different neighbourhoods appear when visualizing the same data (number of cases):
```{r tmap breaks}
tm_shape(neighbourhood_covid) +
tm_fill("n",style="equal", n=5,palette="YlOrRd")+
tm_borders("black", lwd=0.5)+
tm_layout(main.title = "Equal Interval", inner.margins = c(0, 0, 0, 0))
tm_shape(neighbourhood_covid) +
tm_fill("n",style="quantile", n=5,palette="YlOrRd")+
tm_borders("black", lwd=0.5)+
tm_layout(main.title = "Quantile Breaks", inner.margins = c(0, 0, 0, 0))
tm_shape(neighbourhood_covid) +
tm_fill("n",style="jenks", n=5,palette="YlOrRd")+
tm_borders("black", lwd=0.5)+
tm_layout(main.title = "Natural Jenks", inner.margins = c(0, 0, 0, 0))
```
## By Age
```{r by age}
covid_age <- covid %>% group_by(Neighbourhood.Name,Age.Group)%>%
count()
toronto_age<- toronto %>%full_join(covid_age, by="Neighbourhood.Name")
tm_shape(toronto_age) +
tm_fill("n", title = "Total Cases",style ="pretty",n=6)+
tm_facets(by="Age.Group")#<<
```
## Saving
Assign map to a variable `outcomes_map`.
Use `tmap_save` to save the map to your hard drive.
```{r save}
outcomes_map <- tm_shape(toronto_outcomes) +
tm_fill(c("ACTIVE","FATAL"),breaks = outcome_breaks,title="Cases")+
tm_borders("black", lwd=0.5)+
tm_layout(panel.labels =c("Active Cases","Fatal Cases"),legend.outside = TRUE
)
tmap_save(outcomes_map, filename= "outcomes_map.png", width= 600, height= 800)
```