3  Lab: Spatial data in R

In this computer practical you will get familiar with the main R packages to handle spatial data, how to inspect the content and how to plot spatial data.

3.1 Install required software

install.packages("spDataLarge", repos = "https://geocompr.r-universe.dev")
# alternative command for newer R version
# install.packages("spDataLarge", repos = "https://nowosad.github.io/drat/", type = "source")

l.p <- c("ggplot2", "mapview", "sf", "sp", "terra", "tidyterra")
install.packages(l.p)

3.2 Spatial data types

The main spatial data types used for geostatistical tasks are point vector geometries and raster data.

TipTask

Familiarize yourself with spatial data types. Read through Pebesma and Bivand, 2023. Section 1, Getting started.

Coordinate reference systems (CRS) are often referred to by a identifying number (EPSG). An overview of worldwide CRS can be found here https://epsg.io.

3.3 Package sf

R package sf handles geographical vector data. Get familiar with sf objects by reading Pebesma and Bivand, 2023. Section 7.1 (up to and including Subsetting).

TipTask

Load the landslide dataset lsl from package spDataLarge. Create a spatial sf object using st_as_sf() and assign the correct CRS.

Hint

Information on the dataset can be found on the help page that is built in the R package spDataLarge. It can be retrieved by entering ?lsl in the R console or the search in the ‘Help’ tab of RStudio. In the code example at the bottom of this help page you find the code how to read in the lsl dataset using the data() function.

To investigate what is in the dataset you can use the structure command str().


TipTask

Extract the spatial coordinates as vectors (this is often needed to do non-spatial plotting or handing over to functions that cannot deal with sf objects).


CautionQuestion 3-1

Which statement about an sf object in R is correct? Maybe check with str(). (select all that apply)

  1. An sf object is a completely new data structure that cannot be treated as a data.frame. Elements have to be accessed with @.

  2. An sf object is a data.frame with one column containing geometries and additional spatial attributes belonging to this geometries.

  3. An sf object is a list of geometries that must be accessed via st_geometry() only.

 

TipTask

Create a simple plot of the landslide points (column lslpts where true = landslide initiation points; false = unaffected, no landslide).

Also create a plot using the mapview package (use the argument zcol = "lslpts" to display landslide occurence).

CautionQuestion 3-2

Inspect the map display. Which statement is true? (select all that apply)

  1. The data was recorded in a densely populated area.

  2. The data was recorded in a steep mountainous area.

  3. Many landslides very likely damaged a nearby road.

3.4 Package terra

R package terra handles geographical raster data. An alternative is the stars package which is yet less widespread. The predecessor of terra is the no longer supported raster package.

Read in the elevation data from spDataLarge.

library(terra)
ta <- rast(system.file("raster/ta.tif", package = "spDataLarge"))
TipTask

Inspect the content of the elevation raster. Access e.g. the first 10 pixel values of one layer by using values().

Optional: Set all values > 2000 m in the elevation raster to NA.

TipTask

Create a simple plot of the slope (first band).

3.5 Creating maps

Being able to display geographical data prevents erroneous analysis. Besides classical x-y-plotting, R offers a large variety of display options for spatial data. Above you already created maps using plot function. Besides, we will use ggplot2 and mapview packages.

The R package tmap also offers many possibilities, if interested check out Lovelace et al. 2024, Chapter Making Maps with R.

Overlay plots with basic plot functions

TipTask

Using the elevation and landslide data loaded above: create a map showing the slope and overlay the landslide observations points (points()).

Overlay plots with ggplot2

For mapping with ggplot2 see option in chapter Pebesma and Bivand, 2023. Maps with ggplot2. For terra objects see here tidyterra. For extended examples to map with ggplot2 see Wickham, et al., 2024.

TipTask

Plot the elevation and landslide data using ggplot2. To display terra objects with ggplot2 use geom_spatraster() from the tidyterra package. sf objects can be plotted with geom_sf() using the color aesthetics aes(col = lslpts).

3.6 Package sp

The R package sp has been replaced by the easier to use and more powerful sf package (published by the same authors). Geostatistical R packages, however, still depend on spatial sp objects and sometimes require sp objects as input.

How to transform sf to sp and vice versa see Pebesma and Bivand, 2023. Appendix A — Older R Spatial Packages.

TipTask

Load the dataset meuse from package sp. Create a spatial object and assign the correct CRS.

See Example section on the dataset’s help page (?meuse or run example(meuse) to directly execute it).

TipTask

Plot the meuse observation locations using plot. Plot the topsoil zinc concentrations with spplot (= special plot function for sp objects).

TipTask

Create an interactive map displaying the zinc concentration using the mapview package (use zcol = "zinc"). Change the view to aerial imagery and inspect the point locations.

TipTask

Similarly, load meuse.grid. Create a sp raster object (SpatialPixelsDataFrame, see example code on help page of meuse.grid). Explore the content by printing and plotting.