R for Geospatial Professionals

Preface

This web text can be viewed in a light mode or a dark mode. A toggle is provided in the sidebar.

License

CC BY-NC-SA 4.0 license badge.

Creative Commons BY-NC-SA 4.0 license badge

Copyright © 2026 Aaron Maxwell.

Unless otherwise noted, R for Geospatial Professionals by Aaron Maxwell is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License (CC BY-NC-SA 4.0). You are free to share and adapt this material for non-commercial purposes, provided that you give appropriate credit to the original source, provide a link to the license, indicate if changes were made, and distribute any adapted material under the same license.

Goals of Text

This text is meant to introduce geospatial professionals to the R language and computational environment. It covers base R along with the tidyverse packages, including readr, tibble, dplyr, forcats, stringr, lubridate, and ggplot2, for data preparation, aggregation, and visualization. We then explore reading, processing, visualizing, and analyzing geospatial data. For vector data, we primarily focus on the sf package while the terra package is used for raster data. Light detection and ranging (lidar) data processing and analysis is explored with the lidR and arbor packages. Note that all of these chapters, with some minor alterations, are also included in our Geospatial Supervised Learning using R book. We have extracted these chapters to a separate text for those who are not interested in supervised learning specifically or who are more interested in working with geospatial data and performing spatial analysis operations in R.

Use of AI Tools

Portions of this text were developed with the assistance of Claude, an AI model developed by Anthropic, used through the Claude Code tool in 2026. Claude drafted the initial versions of several chapters, including the chapters on arbor, inference, and spatial statistics. It also drafted several exercise sets, conceptual figures, and sections of other chapters (e.g., regular expressions), and it assisted with reorganizing and renumbering the text. All AI-generated content was reviewed, tested, revised, and approved by the authors, who designed the scope and structure of the text, selected the datasets and methods, executed and verified all code, and take full responsibility for the content, including any errors. Claude is not an author of this work.

Error Statement

The authors have tried their best to provide accurate and complete information. Any errors are unintentional. If you would like to comment on any material in this text, please feel free to contact the lead author, Aaron Maxwell, at Aaron.Maxwell@mail.wvu.edu.

Audience

This text was written for two audiences.

  1. Geospatial professionals interested in developing or further developing their spatial analysis skills and learning to work in the R environment.
  2. General data scientists and analysts needing to learn more about working with geospatial data in R.

The material covered in this text may be appropriate for an advanced undergraduate course or a graduate-level course. Since the focus is code implementation and practical considerations, a more theoretical companion text may be necessary.

Structure of Text

This text consists of 19 chapters organized into two parts: Base R and the tidyverse and Working with Geospatial Data in R. Each chapter consists of examples with associated code and explanations. The covered topics are listed at the beginning of each chapter in the Topics Covered section. At the end of most chapters, we provide review questions and exercise(s). Some of the short, more demo-focused chapters do not have associated questions and/or exercises. The data to complete the exercises have been provided or linked, which is described below.

The Base R and the tidyverse section introduces using R for general data science tasks and for working with geospatial data. The goal of this part is to support users in learning R for general data science. 1  Base R (Part 1) covers the basics of the R language while 2  Data Wrangling with the tidyverse covers data manipulation, wrangling, and summarization using the tidyverse packages (e.g., readr, tibble, dplyr, stringr, and forcats). 3  Base R (Part 2) further explores the R language and discusses functions, loops, and control flow. 4  Working with Dates and Times discusses working with dates, times, and time series data using base R and the lubridate package. 5  Graphing with ggplot2 (Part 1) and 6  Graphing with ggplot2 (Part 2) explore the use of ggplot2 for building data visualizations and graphs. 7  Producing Tables (gt) introduces the gt package for building and refining tables. 8  Statistical Inference provides a broad overview of how to implement statistical tests in R.

The Working with Geospatial Data in R section focuses on geospatial data specifically. 9  Static Maps (tmap) introduces mapping and spatial data visualization with tmap while 10  Vector Geoprocessing (sf) focuses on working with and analyzing vector geospatial data with the sf package. 11  Raster Geoprocessing (terra) discusses general raster data handling and processing using the terra package. Continuing the theme of 11  Raster Geoprocessing (terra), 12  Imagery and Digital Terrain Data explores processing multispectral imagery including creating normalized difference band ratios (e.g., the normalized difference vegetation index (NDVI)), performing a tasseled cap transformation, implementing principal component analysis (PCA) for feature reduction, and generating spatial enhancement using convolution filters or kernels. This chapter also discusses creating land surface parameters (LSPs) from digital terrain models (DTMs). 13  Downloading Remotely Sensed Data focuses on downloading Earth observation and digital terrain data stored within a SpatioTemporal Asset Catalog (STAC). 14  Lidar Processing (lidR) discusses the processing and rasterization of lidar point cloud data using the lidR package while 15  Lidar for Forestry (arbor) focuses, more specifically, on the processing of terrestrial laser scanning (TLS) and mobile laser scanning (MLS) data for forestry applications using the arbor package. 16  Using WhiteboxTools in R offers a demo of how R can interface with other geospatial software tools using WhiteboxTools as an example. 17  Geographic Object-Based Image Analysis (GEOBIA) explores an implementation of geographic object-based image analysis (GEOBIA) in R. 18  Spatial Statistics introduces spatial statistics, including point pattern analysis, spatial autocorrelation, semivariograms, and hot spot analysis. The final chapter, 19  Interactive Web Maps (leaflet), introduces building interactive web maps using the leaflet package.

Data and Source Files

This book was written using Quarto. To learn more about Quarto, we recommend Quarto for Scientists by Nicholas Tierney, which was also written using Quarto and has been made available free-of-charge. All the Quarto (.qmd) files that constitute this book can be downloaded from the text’s landing page.

We have also provided the required source data and Quarto files to execute the provided code. These data can be downloaded using the download options in the sidebar. Separate downloads have been provided for each chapter. You will need to uncompress the data before use. If you open a Quarto file from the downloaded directory, the relative file paths used in the code should work so that the paths do not need to be updated. In some cases, the used files are large or already available on the web. In such cases, we note the source at the beginning of the chapter in which they are used.

All data are housed in the gslrData folder. Each chapter has an associated folder in this directory (e.g., chpt1). Within each chapter directory, needed data to execute the chapter code are provided in the data folder while files for the associated exercise(s) are in the exercise folder. We also include initially empty output and scratch folders in each chapter folder for use when the code is executed.

We generally recommend using an integrated development environment (IDE). We suggest RStudio by Posit, which is freely available.

Installing R packages is generally easy. In code, this can be accomplished with install.packages("INSERT PACKAGE NAME"). In RStudio, packages can be installed using Tools –> Install Packages.

Thanks

This online text was partially funded by AmericaView. AmericaView is a nationwide, university-based, and state-implemented network that advances Earth observation education through remote sensing science, applied research, workforce development, technology transfer, and community outreach. AmericaView is supported by the United States Geological Survey (USGS) under Grant/Cooperative Agreement No. G23AP00683. The views and conclusions contained in this text are those of the authors and should not be interpreted as representing the opinions or policies of the U.S. Geological Survey. Mention of trade names or commercial products does not constitute their endorsement by the U.S. Geological Survey.

Partial funding was also provided by the National Science Foundation (NSF) (Federal Award ID No. 2046059: “CAREER: Mapping Anthropocene Geomorphology with Deep Learning, Big Data Spatial Analytics, and LiDAR”). Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.

Nomenclature

We use the following type standards throughout the text:

  1. Bold text is used to highlight key or important terms at first use in each chapter (e.g., Landsat).
  2. Underlined text is used for R package or metapackage names (e.g., terra).
  3. Italic text is used for file names, folder paths/directories, and book or journal titles (e.g., rasterStack.tif).
  4. Table, data frame, or tibble column names are written in “quotes” and italicized (“Band1”).
  5. “Quotes” are placed around names of journal articles.
  6. In-text code snippets and variable names are written as fixed-width text (e.g., var1 = c(1,2,3,4)).
  7. Callout blocks are used to highlight key information, offer practical advice, or relate concepts across chapters.

This is an example callout block.

Concluding Remarks

We hope you are excited to start learning to use R to work with and analyze geospatial data. In the next chapter, we begin by exploring the base R language.

Appendix: Package List

Below is a list of the key packages used in this text with a brief description of their use and links to their documentation.

Package Metapackage Description
Data prep and wrangling
readr (site) tidyverse Read tabulated data
tibble (site) tidyverse Define tibble data type
dplyr (site) tidyverse General data wrangling, filtering, query, and manipulation
stringr (site) tidyverse Work with string/character data
forcats (site) tidyverse Work with factor data
ggplot2 (site) tidyverse Data visualization and graphing
lubridate (site) tidyverse Date and time data handling
purrr (site) tidyverse Functional programming in R
gt (site) - Create tables
Geospatial
terra (site) - Raster geospatial data reading and processing
sf (site) - Vector geospatial data reading and processing
tmap (site) - Thematic map creation
leaflet (site) - Interactive web map creation
lidR (site) - Lidar data processing and handling
arbor (site) - Terrestrial and mobile lidar data processing and handling for forestry
rstac (site) - Download geospatial and Earth observation data stored in a SpatioTemporal Asset Catalog (STAC)
supercells (site) - Create image objects for geographic object-based image analysis (GEOBIA)
exactextractr (site) - Summarize raster data relative to polygon areas
GLCMTextures (site) - Calculate textural measures using the gray-level co-occurrence matrix (GLCM)
spatialEco (site) - Additional functionality for processing geospatial data with a focus on spatial ecology applications
RStoolbox (site) - Additional tools for working with remotely sensed data
MultiscaleDTM (site) - Configure moving windows/kernels for digital terrain analysis
whitebox (site) - Interface to WhiteboxTools
spdep (site) - Spatial dependency and spatial statistics
spatstat (site) - Spatial statistics
gstat (site) - Spatial statistics