-
Notifications
You must be signed in to change notification settings - Fork 12
/
slides.Rmd
666 lines (431 loc) · 14.3 KB
/
slides.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
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
---
title: "Geocomputation with R"
subtitle: "pre-conference workshop"
author: "Robin Lovelace, University of Leeds, for the RGS, London"
date: "2019-04-06. Source code: [github.com/geocompr/geocompkg](https://github.com/geocompr/geocompkg/blob/master/slides/)<br> "
output:
xaringan::moon_reader:
nature:
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
bibliography:
- rg.bib
- geocompr.bib
---
```{r get-refs, include=FALSE}
download.file("https://github.com/geocompr/geocompkg/raw/master/slides/refs-geostat.bib", "rg.bib")
download.file("https://github.com/Robinlovelace/geocompr/raw/master/refs.bib", "geocompr.bib")
refs_geostat = RefManageR::ReadBib("rg.bib")
refs_geocompr = RefManageR::ReadBib("geocompr.bib")
o = ls()
r = o[grepl(pattern = "refs_", x = o)]
refs = do.call(what = c, args = mget(r))
ref_df = as.data.frame(refs)
# View(ref_df)
# RefManageR::WriteBib(refs, "refs-all.bib")
```
class: large
## What is this workshop about?
27 August 2019
2.00pm-5.00pm Free - places must be booked in advance
Royal Geographical Society (with IBG), 1 Kensington Gore, London, SW7 2AR
Free
This event, sponsored by the Quantitative Methods Research Group and led by Dr. Robin Lovelace, will bring together data, maps and (soon-to-be) R enthusiasts to learn a bit more about Robin's latest book, “Geocomputation in R”. In the spirit of open-source hacking, the book is available online and the event will borrow those ideas to offer an introductory, hands-on, and fun session getting started with spatial data in R. The only things you need to bring are your laptop and a positive attitude to learn!
--
```{r, echo=FALSE}
kensington_geo_talk = list(
introduction = "2-3 pm: introduction to R",
ecosystem = "3-4 pm: spatial data in R",
vizpkgs = "4-5 pm: visualisation, map making + off piste"
)
```
```{r}
str(kensington_geo_talk)
```
---
# intro
---
## 'Team geocompr'
```{r setup, include = FALSE}
options(htmltools.dir.version = FALSE)
library(RefManageR)
BibOptions(check.entries = FALSE,
bib.style = "authoryear",
cite.style = 'alphabetic',
style = "markdown",
first.inits = FALSE,
hyperlink = FALSE,
dashed = FALSE)
my_bib = do.call(what = c, args = mget(r))
knitr::opts_chunk$set(eval = TRUE)
```
.pull-left[
- [Jakub Nowosad](https://nowosad.github.io/): developer of GeoPAT + more.
- [Jannes Muenchow](http://www.geographie.uni-jena.de/en/Muenchow.html), creator of **RQGIS**.
- [Robin Lovelace](https://www.robinlovelace.net/), creator of **stplanr**, co-author of Efficent R Programming.
]
.pull-right[
```{r}
library(leaflet)
popup = c("Robin", "Jakub", "Jannes")
m = leaflet() %>%
addProviderTiles("NASAGIBS.ViirsEarthAtNight2012") %>%
addMarkers(lng = c(-3, 23, 11),
lat = c(52, 53, 49),
popup = popup)
```
]
--
```{r, out.height="200px", out.width="700px"}
m
```
---
.pull-left[
## Why use geographic data?
- Geographic data is everywhere
- underlies some of society's biggest issues
- Give generalised analyse local meaning
]
--
.pull-right[
```{r, echo=FALSE}
knitr::include_graphics("https://raw.githubusercontent.com/npct/pct-team/master/figures/rnet-newcastle-cost-road.png")
```
- Example: Propensity to Cycle Tool (PCT) in London: http://pct.bike/m/?r=london
]
---
## What is Geocomputation?
.pull-left[
> GeoComputation is about using the various different types of geodata and about developing relevant geo-tools within the overall context of a 'scientific' approach `r Citep(my_bib, "openshaw_geocomputation_2000", .opts = list(cite.style = "authoryear"))`.
]
.pull-right[
```{r, echo=FALSE, message=FALSE}
knitr::include_graphics("http://www.ccg.leeds.ac.uk/people/s.openshaw/s.openshaw.png")
```
]
--
- But we do differ from early definitions in one important way:
> At the turn of the 21st Century it was unrealistic to expect readers to be able to reproduce code examples, due to barriers preventing access to the necessary hardware, software and data `r Citep(my_bib, "lovelace_geocomputation_2019", .opts = list(cite.style = "authoryear"))`
---
## Another definition
> about harnessing the power of modern computers to *do things* with geographic data `r Citep(my_bib, "lovelace_geocomputation_2019", .opts = list(cite.style = "authoryear"))`.
--
.pull-left[
```{r, echo=FALSE}
knitr::include_graphics("https://geocompr.robinlovelace.net/figures/08_metro_areas.png")
```
]
.pull-right[
Raster image of Germany (resolution: 20 km) with the identified metropolitan areas (golden polygons) and the corresponding names.
]
---
.pull-left[
## What's in the box?
- Chapter 1: History + 'philosophy' = important
Foundations
- Starting from nothing
- Class definitions
- Spatial/attribute operations
- Projections
- Data IO
Extensions
- Advanced methods
- How to build your own functions
Applications
- A taster of what you can do
- Transport, location analysis, ecology
]
--
.pull-right[
## Win this book!
Enter the reproducible map comp:
- most creative map making people think in new ways about geographic phenomena
- most policy relevant map
- Submission: [GitHub](https://github.com/Robinlovelace/geocompr/issues/437) by 16:30
```{r, echo=FALSE, message=FALSE}
knitr::include_graphics("https://geocompr.robinlovelace.net/images/cover.png")
```
]
---
## Let's get started with R
--
But first some audience participation
<!-- Get people to stand up on one leg, hold their ear, rub their bellies, rub their bellies and heads. Point: it will be hard. Plus bonding -->
--
Who has used R before?
--
Who has never used R before?
--
Who installed R on their computer for this workshop?
--
Who hasn't got R installed?
--
Any questions?
--
5 minute break + set-up -> practical bit
---
---
## A brief history of geographic packages in R
- See a [video](https://www.youtube.com/watch?v=Luxllb_WAqY) of Roger Bivand's talk on the subject (live demo of [R 0.49](https://cran.r-project.org/src/base/R-0/), released 1997) + GitHub [repo](https://github.com/rsbivand/eRum18/blob/master/eRum_keynote_18.Rmd)
- Brian Ripley, on R's [core team](https://www.r-project.org/contributors.html), with strong interest in spatial stats
- Rs capabilities have evolved substantially since then!
--
- **rgdal**, released in 2003
--
- **sp**, released 2005
- **sp**verse: **gstat**, **geosphere**, **adehabitat**
- **raster**, released 2010
- **sf**, released 2017
---
## Getting data
- Many portals and packages for getting data - see [Chapter 7](https://geocompr.robinlovelace.net/read-write.html)
```{r}
library(osmdata)
kensington = getbb("kensington, london")
osm_data = opq(bbox = kensington) %>%
add_osm_feature(key = "leisure", value = "park") %>%
osmdata_sf()
parks = osm_data$osm_polygons
```
--
```{r, echo=FALSE, warning=FALSE, message=FALSE, out.height="200px"}
library(tmap)
tmap_mode("view")
qtm(parks)
tmap_mode("plot")
```
---
## Getting transport data
- pct package developed to make transport data more accessible
--
- Get transport zones for kensington
```{r}
zones_all = read_sf("zones_near_rgs.geojson")
zones = zones_all[c("geo_code", "car_driver", "foot", "bicycle")]
plot(zones)
```
---
## sf
- **sf** is a recently developed package for spatial (vector) data
- Combines the functionality of three previous packages: **sp**, **rgeos** and **rgdal**
- Has many advantages, including:
- Faster data I/O
- More geometry types supported
- Compatibility with the *tidyverse*
---
## Reading and writing spatial data
```{r, echo=FALSE}
library(sf)
crashes_near_rgs = read_sf("crashes_near_rgs.geojson")
```
- `write_sf()` writes data `st_write(zones, 'zones.gpkg')`.
- See supported formats with: `sf::st_drivers()`. Details: Chapter 6 of our book: [geocompr.robinlovelace.net/read-write.html](https://geocompr.robinlovelace.net/read-write.html)
---
## sf class
```{r}
library(spData) # pre-packaged data
class(zones)
zones
```
```{r, eval=FALSE, echo=FALSE}
# ---
## Structure of the sf objects
# world$name_long
# ```
#
# ```{r, echo=FALSE}
# world$name_long[1:3]
# ```
#
# ```{r, eval=FALSE}
# world$geom
# ```
#
# ```{r, echo=FALSE}
# print(world$geom, n = 3)
```
---
## Spatial operations
It's a big topic which includes:
- Spatial subsetting
- Spatial joining/aggregation
- Topological relations
- Distances
- Spatial geometry modification
- Raster operations (map algebra)
See [Chapter 4](http://geocompr.robinlovelace.net/spatial-operations.html) of *Geocomputation with R*
---
## Spatial subsetting
```{r, warning=FALSE}
parks = read_sf("parks.geojson")
crashes_in_parks = crashes_near_rgs[parks, ]
```
--
```{r, echo=FALSE, message=FALSE}
tmap_mode("view")
tm_shape(crashes_near_rgs) +
tm_dots(alpha = 0.2, size = 0.1) +
tm_shape(parks) +
tm_polygons(col = "green", alpha = 0.3) +
tm_shape(crashes_in_parks, col = "red") +
tm_dots()
```
---
## Spatial subsetting II
```{r, warning = FALSE, message = FALSE, fig.height = 4}
lnd_buff = lnd[1, ] %>%
st_transform(crs = 27700) %>% # uk CRS
st_buffer(500000) %>%
st_transform(crs = 4326)
near_lnd = world[lnd_buff, ]
plot(near_lnd$geom)
```
- What is going with the country miles away?
---
## Spatial aggregation
```{r}
points_per_zone = aggregate(crashes_near_rgs[1], zones, length)
plot(points_per_zone)
```
---
## Exercises
- Find Chapter 4 of Geocomputation with R online
- Complete Section 4.2, which will involve running (at a minimum) the following code:
```{r, message=FALSE}
library(sf)
library(spData)
library(dplyr)
canterbury = nz %>% filter(Name == "Canterbury")
canterbury_height = nz_height[canterbury, ]
```
- Attempt Exercises 1:3 at the end of the chapter
- Bonus: find out how to get the data for the RGS site and identify all crashes that happened within 200 m of the RGS building in 2017
---
background-image: url(https://user-images.githubusercontent.com/1825120/38543030-5794b6f0-3c9b-11e8-9da9-10ec1f3ea726.gif)
background-size: cover
class: center, middle
# Geographic data visualisation
--
Source: [Geocomputation with R](https://geocompr.robinlovelace.net/adv-map.html#animated-maps)
---
## sf graphics
```{r}
plot(zones[c("car_driver", "foot")])
```
---
## ggplot2
```{r}
library(ggplot2)
ggplot(zones) +
geom_sf(aes(fill = bicycle)) +
scale_fill_viridis_c()
```
---
## leaflet
```{r, eval=FALSE}
library(leaflet)
leaflet(zones) %>%
addTiles() %>%
addPolygons(color = "#444444", weight = 1, fillOpacity = 0.5,
fillColor = ~colorQuantile("YlOrRd", bicycle)(bicycle),
popup = paste(round(zones$bicycle, 2)))
```
```{r, echo=FALSE, message=FALSE}
library(widgetframe)
library('leaflet')
l = leaflet(zones) %>%
addTiles() %>%
addPolygons(color = "#444444", weight = 1, fillOpacity = 0.5, fillColor = ~colorQuantile("YlOrRd", foot)(foot), popup = paste(round(zones$foot, 2)))
frameWidget(l, height = '400')
```
---
## tmap
- A dedicated mapping R package
```{r}
library(tmap)
tm_shape(zones) +
tm_polygons("foot", palette = "RdYlBu")
```
---
## tmap II
- A dedicated mapping R package
```{r, message=FALSE}
tmap_mode("view")
tm_shape(zones) +
tm_polygons("foot", palette = "RdYlBu")
```
---
## Mapdeck
- See example at [geocompr.robinlovelace.net/adv-map.html](https://geocompr.robinlovelace.net/adv-map.html#other-mapping-packages)
```{r}
library(mapdeck)
set_token(Sys.getenv("MAPBOX"))
mapdeck(data = zones) %>%
mapdeck::add_sf() %>%
add_polygon(elevation = zones$foot^2)
```
---
## Shiny
Here's one I made earlier: www.pct.bike
```{r, echo=FALSE}
knitr::include_graphics("https://raw.githubusercontent.com/npct/pct-team/master/figures/front-page-leeds-pct-demo.png")
```
---
## Future: Advances in raster data with R
Raster data in R is evolving:
- **raster** package dominates, designed for `Spatial*` objects, e.g. with `as(my_vector, "Spatial")`
- Efforts to bring raster data closer to the **tidyverse**/**sf** approaches, such as [tabularaster](https://github.com/hypertidy/tabularaster), [sfraster](https://github.com/mdsumner/sfraster) + [fasterize](https://github.com/ecohealthalliance/fasterize)
- [stars](https://github.com/r-spatial/stars) focusses on multidimensional space-time (raster/vector) is evolving.
- **[terra](https://github.com/rspatial/terra)** - first release expected 2019
--
- See https://github.com/edzer/sdsr
```{r, out.width="200px"}
raster::plot(spData::elev)
```
---
## Spatial networks
- Lots of packages out there, including `stplanr`, `dodgr` and `tidygraph`
- Source: https://github.com/spnethack/spnethack
```{r, echo=FALSE, out.width="80%"}
knitr::include_graphics("https://raw.githubusercontent.com/spnethack/spnethack/master/README_files/figure-gfm/pway-1.png")
```
---
## Summary: do things with geographic data in R
Base R + **tidyverse** now (mostly) works with spatial data.
This is thanks to **sf**, a recent package (first release in 2016) that implements the open standard data model *simple features*. Get **sf** with:
```{r, eval=FALSE}
install.packages("sf")
```
Raster data is also supported, in the more mature package **raster**:
```{r, eval=FALSE}
install.packages("raster")
```
For datasets...:
```{r, eval=FALSE}
install.packages("spData")
install.packages("rnaturalearth")
```
For more on this see the book: [github.com/Robinlovelace/geocompr](https://github.com/Robinlovelace/geocompr)
--
- 2 day workshop: [lida.leeds.ac.uk/event](https://lida.leeds.ac.uk/event/geocomputation-data-analysis-r/)
---
## Final practical session
- Option 1: Continue to work through exercises in the book - pick a chapter!
- Option 2: Work on reading in and mapping data related to your research (and ask for help)
- Everyone: Get a GitHub account and submit something to the reproducible map competition here: [github.com/robinlovelace/geocompr/issues](https://github.com/robinlovelace/geocompr/issues)
---
class: center, middle
# Thanks, links, happy (R)GS 🚶, 🚲 + 🚀!
- Reproducible slides: [github](https://github.com/geocompr/RGSWorkshop)
--
- Open source book: [geocompr.robinlovelace.net](https://geocompr.robinlovelace.net/)
--
- Support the project + open source: install it, use it, communicate!
--
- Twitter #geocompr, [@robinlovelace](https://twitter.com/robinlovelace), [@jakub_nowosad](https://twitter.com/jakub_nowosad/)
Check out future R courses...
--
```{r, echo=FALSE}
knitr::include_graphics("https://user-images.githubusercontent.com/1825120/55670985-962c7700-5882-11e9-8bef-188854547011.png")
```