Spatial Data Processing with R

Author
Affiliation

Jérôme Guélat, PhD

Published

June 5, 2025

Keywords

GIS, R Spatial, Spatial analyses, Geoprocessing, Cartography, Static maps, Dynamic maps

Preface

The first version of this tutorial was created for a course given at the Swiss Ornithological Institute in 2023. Researchers and students in ecology were the original audience but the whole material can be used by anybody who wants to learn more about how to perform GIS analyses and design maps with the R statistical programming language. The tutorial was heavily updated in 2025.

My goal was to combine a very general and basic introduction to GIS with some sort of cookbook showing how to perform common GIS analyses and create maps with R. The introductory sections should provide enough information for readers with no or little GIS experience in order to understand the rest of the material. You’re welcome to skip them, especially if you understand GIS data models and how GIS data is stored in R.

The cookbook part of the tutorial is a collection of analyses and mapping techniques that I regularly use in my job. Most of them are standard GIS procedures, but you’ll also find more advanced topics. There are two ways to run the code snippets in this tutorial. You can either run everything, starting with the first section and continuing with the others. Or, if you prefer the cookbook approach, I also included a collapsible code snippet at the start of each section that will allow you to get all the required data and load packages for this section only.

You will notice that the amount of content (examples, explanations, exercises, etc.) can be variable depending on the section. I’m sorry for this, this is purely due to a lack of time on my side… I promise to do my best to add more content soon (hopefully).

None of this could have been written without the incredible work done by the R-spatial community. I’d especially like to thank all the software developers who created and are maintaining the R software, the R packages and the open-source libraries used in these packages!

Getting ready

You’ll need the following packages to run the examples in this book. You can use the following code to install them. All the required dependencies will be automatically installed. The most important ones are sf1, terra2, tmap3 and mapview4.

Important

Please also update to the latest version of R, otherwise you may get packages that are not fully up-to-date.

install.packages("sf")
install.packages("terra")
install.packages("tmap")
install.packages("mapview")
install.packages("classInt")
install.packages("httr2")
install.packages("leaflet.extras2")
install.packages("leafpop")
install.packages("leafsync")
install.packages("lwgeom")
install.packages("maptiles")
install.packages("qgisprocess")
install.packages("spatstat.random")
install.packages("spdep")
install.packages("tmaptools")

The required data is available on the GitHub repository (https://github.com/jguelat/spatial-r). You’ll also find all the code needed to re-create the whole book.

Changelog

Only the big updates are listed here.

2025-06-04 New sections showing how to use basemaps in static maps
2025-05-20 New section on the structure of SpatRaster objects
2025-05-15 Short introduction about vector data with terra
2025-05-08 The whole content was converted to a Quarto book and is available as a GitHub website: https://jguelat.github.io/spatial-r.
2025-02-25 Update of the tmap subsections (static and dynamic maps), compatibility with version 4.x, more explanations and map examples.
2025-02-07 Large update of the “Tips and tricks for vectors” and “Dynamic maps” sections, second workshop in Sempach.
2023-01-20 First version of the book for the workshop in Sempach.