forked from gcperk/Rpresentations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsingRForSpatialAnalysis.Rmd
More file actions
488 lines (331 loc) · 13.6 KB
/
UsingRForSpatialAnalysis.Rmd
File metadata and controls
488 lines (331 loc) · 13.6 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
---
title: "Using R for Spatial Analysis"
author: "G Perkins"
date: "March 2, 2019"
output: ioslides_presentation
output:
xaringan::moon_reader:
css: ["default", "default-fonts", "css/my-theme.css"]
chakra: libs/remark-latest.min.js
lib_dir: libs
nature:
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## R Markdown
This is an R Markdown presentation. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document.
## Slide with Bullets
- Bullet 1
- Bullet 2
- Bullet 3
## Slide with R Output
```{r cars, echo = TRUE}
summary(cars)
```
## Slide with Plot
```{r pressure}
plot(pressure)
```
---
title: "R for Geospatial Data Science"
subtitle: "Victoria GeoGeeks Meetup"
author: "Andy Teucher"
date: "2018/01/30"
output:
xaringan::moon_reader:
css: ["default", "default-fonts", "css/my-theme.css"]
chakra: libs/remark-latest.min.js
lib_dir: libs
nature:
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
---
```{r setup, include=FALSE}
options(htmltools.dir.version = FALSE)
knitr::opts_chunk$set(warning = FALSE,
message = FALSE,
cache = TRUE,
fig.align = 'center')
```
background-image: url(img/spreadsheet.png)
???
When Paul invited me I asked him how many people would be R users... and he said maybe none, possibly as many as three. So instead of spending a lot of time convincing you of R's spatial powers (which are many), I'm going to try to convince why using a tool like R is a good idea for any sort of analytical work, including spatial, which by the way R is REALLY good for.
I'm going to start off by describing a pretty common scenario, and then talk about how using **R** can help:
- You're doing a big project for a client. You have a four or five spatial files and a few csvs or Excel sheets containing data related to the spatial layers.
- First you do a bunch of data cleaning in the spreadsheets - fixing date formats, unit conversions, transformations, unify inconsistent variable names, etc.
---
background-image: url(img/arcmap.jpg)
???
- You import the layers into a GIS, do a bunch of spatial operations - intersects, unions, buffers, spatial joins, etc.
- You import the tabular data and join it to the appropriate layers, and do more work.
---
background-image: url(img/arcmap-layout.jpg)
???
- Spend some time creating really nice looking map for final report
---
background-image: url(img/graph.png)
???
- Export the tabular data out so you can summarize it and make some nice graphs in Excel
---
class: middle, center
.pull-left[]
--
# "Thank you for your hard work, this is exactly what we were looking for!"
???
- Combine the results in a word document, convert it to a pdf and give to your client. They thank you for your great work, but say 'Oh, I think I gave you the wrong version of one of the spatial files, and that Excel spreadsheet has been updated.'
---
class: inverse
## "But, I think I gave you the wrong version of shapefile #2. Oh, and that Excel spreadsheet has been updated."
--
.center[]
---
background-image: url(img/full-cycle.png)
???
- Can you redo the analysis exactly the same way? How long will it take you?
---
background-image: url(img/Rlogo.png)
## Open source programming language
## Data analysis focus
## Cross-platform
???
# What is R?
- R is an open-source programming language for data analysis and visualization.
- In many ways it is similar to other general purpose interpreted languages like Python, but unlike Python, R was originally created as a statistical programming language and so it is designed from the ground up around the manipulation, analysis, and visualization of data - it is now often considered the 'lingua franca' of data.
- It also easy to make publication-quality graphics.
- R is available for Windows, MacOS and most popular Linux distributions.
- You can run it interactively by typing directly into the console, or from a sequence of commands written in text files, or "scripts". Scripts can be run against a dataset to produce a defined output, such as a chart or another dataset which contains the results of the analysis.
---
background-image: url(img/efficiency-car.jpg)
# Efficiency
???
So, what can **R** give you?
---
background-image: url(img/full-cycle.png)
???
The entire workflow can be done efficiently in **R**, including:
- reading in raw data from many different sources - spatial and non-spatial,
- preparing the data for analysis, including easily integrating tabular and spatial data,
- performing spatial data manipulation and analysis,
- performing statistical analysis
- creating publication-quality graphs and figures.
---
background-image: url(img/R-flow.png)
???
The entire workflow can be done efficiently in **R**, including:
- reading in raw data from many different sources - spatial and non-spatial,
- preparing the data for analysis, including easily integrating tabular and spatial data,
- performing spatial data manipulation and analysis,
- performing statistical analysis
- creating publication-quality graphs and figures.
- All your iteration happens in one place, and is recorded in scripts
---
background-image: url(img/photocopier.jpg)
# Reproducibility
???
Reproducibilty means:
- *You can easily recreate or update exactly what you did*: In the scenario I described, If you coded the entire above workflow in an R script, you can just change a few lines to change the input data and rerun the scripts.
- *You know what you did*: Doing your data preparation and analysis by coding it in **R** necessarily documents every step. The analysis and documentation are inextricably intertwined.
- *Others know what you did*: They can inspect and re-run your code to understand what you did, verify you've done it correctly, and build on your work.
---
background-image: url(img/k-car.jpg)
# Reliability
???
Using **R** can help you:
- *Minimize errors*: point and click and copy and paste operations are very error-prone, and it's hard to catch those errors
- And you can *find and correct errors* more easily: Changing a bit of code and re-running your script is much more efficient than doing it manually (without making another error).
---
background-image: url(img/swiss-army.jpg)
# Versatility
???
R has a huge array of add-on packages that extend the capabilities of R for performing diverse tasks, including:
- data manipulation,
- data visualization, and
- almost every statistical method you can think of
- With a wide range of packages R also supports advanced geospatial statistics, modeling and visualization.
- Packages are generally published on CRAN - or the "Comprehensive R Archive Network", these packages are easily installable by a user from within R.
---
# Community
.img-right[]
.footnote[['Faces of R' by Maelle Salmon](http://www.masalmon.eu/2017/03/19/facesofr/)]
???
I think one of the best reasons to use **R** is the:
Fantastic online **R** Community
- **R** is hugely popular right now.
- If you have a question or are having a problem, 99% of the time you'll find that it has already been asked and answered online. And if it hasn't, people are almost always willing to help out.
- (rstats, community.rstudio.com, mailing lists, StackOverflow, RWeekly)
---
# Data Science
.center[]
.footnote[Wickham and Grolemund: [R for Data Science](http://r4ds.had.co.nz/)]
???
- hyped word, lots of definities out there
- To me - it's data analysis but inclusive of all steps of the data cycle, with a greater emphasis on programming.
- Spatial analysis can easily be fit into this framework, another side of the same coin.
---
# Geographical Data Science
.center[]
.footnote[Wickham and Grolemund: [R for Data Science](http://r4ds.had.co.nz/)]
???
- movement labelled Geographical Data Science (GDS). Combines ‘data science’ with GIS.
- focus on reproducibility and programming interface
---
### .center[Geographic Information Systems (GIS)</br>vs</br>Geographic Data Science (GDS)<sup>1</sup>]
.middle[
```{r echo=FALSE}
library(readr)
tbl_11.1 <- read_csv('Attribute,GIS,GDS
Home disciplines,Geography,"Geography, Computing, Statistics"
Software focus,Graphical User Interface,Code
Reproducibility,Minimal,Maximal')
knitr::kable(tbl_11.1, format = "html")
```
]
.footnote[
[1] Lovelace, Nowosad, and Muenchow: Geocomputation with R ([Table 1.1](https://geocompr.robinlovelace.net/intro.html#tab:gdsl))
]
???
Why R for spatial?
- Generally the same arguments as I made above for general data analysis.
- reading, writing and manipulating spatial data; making static and interactive maps; and spatial statistics and modelling
- Ability to seamlessly switch between spatial and non-spatial data processing, modeling and visualization tasks
---
background-image: url(img/vic-open-data.png)
---
background-image: url(img/dog.png)
---
```{r include=FALSE, eval=FALSE}
temp <- tempfile()
download.file("http://vicmap.victoria.ca/_GISData/DogsByNeighbourhoodSHP.zip", destfile = temp)
unzip(temp, exdir = "data/dogs")
```
```{r}
library(sf)
library(mapview)
dogs <- read_sf("data/dogs/DogsByNeighbourhood.shp")
mapview(dogs)
```
???
sf - new package that implements the simple features spec in R for representing spatial data
- Binds to GDAL for reading and writing data, to GEOS for geometrical operations, and to Proj.4 for projection conversions and datum transformations
- can read and write almost any format
---
```{r}
library(ggplot2)
ggplot() +
geom_sf(data = dogs, aes(fill = DogLicence)) +
scale_fill_viridis_c() + theme_minimal()
```
???
Plotting - built-in in sf, even better in ggplot2
---
background-image: url(img/hydrant.png)
---
```{r include=FALSE, eval=FALSE}
temp <- tempfile()
download.file("http://vicmap.victoria.ca/_GISData/HydrantsSHP.zip", destfile = temp)
unzip(temp, exdir = "data/hydrants")
```
```{r}
hydrants <- read_sf("data/hydrants/Hydrants.shp")
ggplot() +
geom_sf(data = hydrants) +
theme_minimal()
```
---
```{r}
hydrants <- st_intersection(hydrants, st_geometry(dogs))
ggplot() +
geom_sf(data = dogs, aes(fill = DogLicence)) +
geom_sf(data = hydrants, colour = "white") +
scale_fill_viridis_c() + theme_minimal()
```
???
### geometry generating logical operators
* `st_union`: union of several geometries
* `st_intersection`: intersection of pairs of geometries
* `st_difference`: difference between pairs of geometries
* `st_sym_difference`: symmetric difference (`xor`)
---
```{r eval=require(dplyr)}
dogs_and_hydrants <- st_join(dogs, hydrants) %>%
group_by(Neighbourh) %>%
summarize(licences = max(DogLicence),
n_hydrants = length(unique(ID)),
hydrants_per_dog = n_hydrants / licences)
```
```{r echo=FALSE}
knitr::kable(st_set_geometry(dogs_and_hydrants, NULL), format = "html")
```
???
### logical binary geometry predicates
* `st_intersects`: touch or overlap
* `st_disjoint`: !intersects
* `st_touches`: touch
* `st_crosses`: cross (don't touch)
* `st_within`: within
* `st_contains`: contains
* `st_overlaps`: overlaps
* `st_covers`: cover
* `st_covered_by`: covered by
* `st_equals`: equals
* `st_equals_exact`: equals, with some fuzz
### higher-level operations: summarise, interpolate, aggregate, st_join
* `aggregate` and `summarise` use `st_union` (by default) to group feature geometries
* `st_interpolate_aw`: area-weighted interpolation, uses `st_intersection` to interpolate or redistribute attribute values, based on area of overlap:
* `st_join` uses one of the logical binary geometry predicates (default: `st_intersects`) to join records in table pairs
### manipulating geometries
* `st_line_merge`: merges lines
* `st_segmentize`: adds points to straight lines
* `st_voronoi`: creates voronoi tesselation
* `st_centroid`: gives centroid of geometry
* `st_convex_hull`: creates convex hull of set of points
* `st_triangulate`: triangulates set of points (not constrained)
* `st_polygonize`: creates polygon from lines that form a closed ring
* `st_simplify`: simplifies lines by removing vertices
* `st_split`: split a polygon given line geometry
* `st_buffer`: compute a buffer around this geometry/each geometry
* `st_make_valid`: tries to make an invalid geometry valid (requires lwgeom)
* `st_boundary`: return the boundary of a geometry
---
```{r}
ggplot() +
geom_sf(data = dogs_and_hydrants, aes(fill = hydrants_per_dog)) +
scale_fill_viridis_c() + theme_minimal()
```
---
```{r fig.width=8}
ggplot(dogs_and_hydrants, aes(x = Neighbourh, y = hydrants_per_dog)) +
geom_col(aes(fill = licences)) + scale_fill_viridis_c() +
coord_flip() + theme_minimal()
```
---
.pull-left[
## Me:
Twitter: @andyteucher
GitHub: @ateucher
]
.pull-right[
## Resources:
http://fg2re.sellorm.com/
http://r4ds.had.co.nz/
https://geocompr.robinlovelace.net/
http://r-spatial.org/
]
.footnote[Source code for this presentation available [here](https://github.com/ateucher/vic-geogeeks-2018-01-30)
Made with [xaringan](https://slides.yihui.name/xaringan/)
]
---
```{r eval=require(leaflet)}
leaflet() %>%
addProviderTiles("Stamen.TonerLite") %>%
addAwesomeMarkers(lng = -123.369407, lat = 48.428881,
label = paste0("<h3>", emo::ji("beer"), "</h3>"),
labelOptions = labelOptions(noHide = TRUE))
```
© 2019 GitHub