+ - 0:00:00
Notes for current slide
Notes for next slide

R for Spatial Analysis: futureproof foundations

🌐
from statistical language to GIS

Robin Lovelace

2017-10-10

1 / 22

Introduction

This one day course will get you up-to-speed with using R and RStudio for daily working with spatial data. You will learn about R's powerful geospatial data processing, analysis and visualisation capabilities. It is practical and hands-on: you'll learn by doing. It assumes you already use R and want to extend your knowledge for spatial data applications. It will cover the recently developed sf package, which is compatible with the tidyverse, representing the cutting-edge of spatial data applications. It will provide a solid foundation (including spatial aggregation, joining, CRSs, visualisation) on which advanced analysis analysis workflows can be built.

install.packages("tidyverse")
2 / 22

Learning outcomes

By the end of the course participants will:

  • Understand R's spatial ecosystem and which packages are 'future proof'
  • Know how to optimize RStudio for productive working with spatial data (you should already be proficient with RStudio)
  • Be able to read and write a variety of spatial data formats
  • Be proficient at common spatial operations including subsetting, cropping, aggregation and transformation
  • Be a confident map maker using the powerful tmap package
  • Know where to look for learning more advanced methods
3 / 22

Prerequisites

In preparation for the course you should:

4 / 22

Test your set-up

This should work:

library(sf)
library(raster)
library(spData)
install.packages("sf")
install.packages("spData")
5 / 22

Course materials

The course will be based on Chapter 4 of Geocomputation with R of the forthcoming book Geocomputation with R plus some additional materials:

6 / 22

Course agenda

Refreshments & set-up: (09:00 - 09:30)

Coffee break: (10:45 - 11:00)

7 / 22
  • Raster data classes and functions (11:00 - 11:15)
  • Raster attribute data (11:15 - 12:00)
  • Free working (12:00 - 12:30)
    • Challenge: working-through and complete the exercises in Chapter 3
    • Bonus: ask a question and help explain something
    • Advanced: look into GitHub and data science

LUNCH and looking at your data (12:30 - 13:30)

8 / 22
  • Worked example: car fleet analysis with Craig Morton (13:30 - 14:15)

  • Spatial data operations (14:15 - 15:00)

    • Practical based on the second handout
    • Spatial subsetting, section 4.2.1
    • Topological relations
    • Spatial joining and aggregation

Coffee break: 15:00 - 15:15

9 / 22
  • Geographic data I/O: (15:15 - 15:30)
    • Taught lecture
    • Test based on your own data
  • Spatial operations on raster data (15:30 - 15:45)

    • Practical - work through section 4.3
  • Consolidating knowledge (15:45 - 16:15)

    • Completing the printed hand-out OR
    • Point pattern analysis, interpolation and rasterization tutorial
    • Working on your own data
  • Wrap-up and next steps (16:15 - 16:30)

10 / 22

R's spatial ecosystem

The popularity of spatial packages in R. The y-axis shows the average number of downloads, within a 30-day rolling window, of R's top 5 spatial packages, defined as those with the highest number of downloads last month.

The popularity of spatial packages in R. The y-axis shows the average number of downloads, within a 30-day rolling window, of R's top 5 spatial packages, defined as those with the highest number of downloads last month.

11 / 22

Before spatial packages...

  • Coordinates were treated as just another variable
  • Issues with visualisation, spatial relations and consistency
  • Major issues with Coordinate Reference Systems (CRSs)
library(spData) # see data(package = "spData") and ?afcon for more info
plot(afcon$x, afcon$y)

12 / 22

sp

  • Released in 2005 (Pebesma and Bivand 2005)
  • Raster and vector data (but mostly raster) supported
  • We won't be using it but it is useful to know about ()
library(sp)
data(meuse)
coords = SpatialPoints(meuse[c("x", "y")])
meuse = SpatialPointsDataFrame(coords, meuse)
str(meuse)
## Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
## ..@ data :'data.frame': 155 obs. of 14 variables:
## .. ..$ x : num [1:155] 181072 181025 181165 181298 181307 ...
## .. ..$ y : num [1:155] 333611 333558 333537 333484 333330 ...
## .. ..$ cadmium: num [1:155] 11.7 8.6 6.5 2.6 2.8 3 3.2 2.8 2.4 1.6 ...
## .. ..$ copper : num [1:155] 85 81 68 81 48 61 31 29 37 24 ...
## .. ..$ lead : num [1:155] 299 277 199 116 117 137 132 150 133 80 ...
## .. ..$ zinc : num [1:155] 1022 1141 640 257 269 ...
## .. ..$ elev : num [1:155] 7.91 6.98 7.8 7.66 7.48 ...
## .. ..$ dist : num [1:155] 0.00136 0.01222 0.10303 0.19009 0.27709 ...
## .. ..$ om : num [1:155] 13.6 14 13 8 8.7 7.8 9.2 9.5 10.6 6.3 ...
## .. ..$ ffreq : Factor w/ 3 levels "1","2","3": 1 1 1 1 1 1 1 1 1 1 ...
## .. ..$ soil : Factor w/ 3 levels "1","2","3": 1 1 1 2 2 2 2 1 1 2 ...
## .. ..$ lime : Factor w/ 2 levels "0","1": 2 2 2 1 1 1 1 1 1 1 ...
## .. ..$ landuse: Factor w/ 15 levels "Aa","Ab","Ag",..: 4 4 4 11 4 11 4 2 2 15 ...
## .. ..$ dist.m : num [1:155] 50 30 150 270 380 470 240 120 240 420 ...
## ..@ coords.nrs : num(0)
## ..@ coords : num [1:155, 1:2] 181072 181025 181165 181298 181307 ...
## .. ..- attr(*, "dimnames")=List of 2
## .. .. ..$ : chr [1:155] "1" "2" "3" "4" ...
## .. .. ..$ : chr [1:2] "x" "y"
## ..@ bbox : num [1:2, 1:2] 178605 329714 181390 333611
## .. ..- attr(*, "dimnames")=List of 2
## .. .. ..$ : chr [1:2] "x" "y"
## .. .. ..$ : chr [1:2] "min" "max"
## ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
## .. .. ..@ projargs: chr NA
13 / 22

sp visuals

12 year-old code still works (Pebesma and Bivand 2005)

plot(meuse, pch=1, cex = .05*sqrt(meuse$zinc))

14 / 22

Packages building on sp

sp has many reverse dependencies:

sp_revdeps = devtools::revdep("sp", dependencies = "Depends")
head(sp_revdeps)
## [1] "adehabitat" "adehabitatHR" "adehabitatHS" "adehabitatLT"
## [5] "adehabitatMA" "automap"
length(sp_revdeps)
## [1] 135
sp_revimps = devtools::revdep("sp", dependencies = "Imports")
length(sp_revimps)
## [1] 256
15 / 22

Where to find out about packages?

Online (dur) but with guidance, e.g. from:

A few of note:

  • adehabitat for ecological modelling
  • geosphere for operations on a spherical surface
  • spdep for modelling spatial data
  • rgdal for reading/writing data
16 / 22

The 5 most downloaded packages that depend on sp

17 / 22

sf

  • Released in November 2016
  • Aims to supersede sp, rgdal and rgeos with unified interface
  • Treats spatial vector data as regular data frames
  • The basis of much of this tutorial and Geocomputation with R
  • Compatible with the tidyverse (Wickham and Grolemund 2016)
library(sf)
## Linking to GEOS 3.5.1, GDAL 2.2.1, proj.4 4.9.2, lwgeom 2.3.3 r15473
world_tbl = dplyr::as_data_frame(world)
world_tbl
## # A tibble: 177 x 11
## iso_a2 name_long continent
## <chr> <chr> <chr>
## 1 AF Afghanistan Asia
## 2 AO Angola Africa
## 3 AL Albania Europe
## 4 AE United Arab Emirates Asia
## 5 AR Argentina South America
## 6 AM Armenia Asia
## 7 AQ Antarctica Antarctica
## 8 TF French Southern and Antarctic Lands Seven seas (open ocean)
## 9 AU Australia Oceania
## 10 AT Austria Europe
## # ... with 167 more rows, and 8 more variables: region_un <chr>,
## # subregion <chr>, type <chr>, area_km2 <dbl>, pop <dbl>, lifeExp <dbl>,
## # gdpPercap <dbl>, geom <simple_feature>
18 / 22

raster

  • A very large package first released in 2010
  • Provides support for raster classes, and user friendly functions for vector data
  • Very powerful
library(raster)
r = raster(nrows = 3, ncols = 3)
values(r) = 1:9
plot(r)

19 / 22

Visualisation packages

  • ggplot2 support for sf
  • leaflet for low-level control of interactive maps
  • mapview for GIS-like feel
  • tmap powerful, flexible, user-friendly, general-purpose map-making
20 / 22

Mini tmap demo

library(tmap)
tmap_mode("view")
## tmap mode set to interactive viewing
qtm(world, fill = "pop")
21 / 22

References

Grolemund, G., Wickham, H., 2016. R for Data Science, 1 edition. ed. O’Reilly Media.

Lovelace, R., Nowosad, J., Meunchow, J., 2018. Geocomputation with R. CRC Press.

Pebesma, E.J., Bivand, R.S., 2005. Classes and methods for spatial data in R. R news 5, 9–13.

Links and example code from the course in Leeds: https://github.com/Robinlovelace/Creating-maps-in-R/blob/master/courses/leeds-2017-10.Rmd

Plug: GIS for Transport Applications: https://www.eventbrite.co.uk/e/gis-for-transport-applications-tickets-38491819067

22 / 22

Introduction

This one day course will get you up-to-speed with using R and RStudio for daily working with spatial data. You will learn about R's powerful geospatial data processing, analysis and visualisation capabilities. It is practical and hands-on: you'll learn by doing. It assumes you already use R and want to extend your knowledge for spatial data applications. It will cover the recently developed sf package, which is compatible with the tidyverse, representing the cutting-edge of spatial data applications. It will provide a solid foundation (including spatial aggregation, joining, CRSs, visualisation) on which advanced analysis analysis workflows can be built.

install.packages("tidyverse")
2 / 22
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow