Spatial Analysis and Mapping with R


This tutorial introduces the reader to some of the amazing capabilities of R to work with and map geographic data. Geographic data are data that contain spatial attributes (or spatial data) that define a geographic space (location, area, elevation, etc.) and non spatial attributes (f.e., population density, pollutant concentrations, temperature).

This tutorial was developed for one the units of the course “ENVS 420: Research Seminar in Environmental Sciences” offered at the University of Baltimore. However, it is hoped that readers outside of ENVS 420 who are interested in geospatial analysis and with a basic familiarity of R find this tutorial useful.

The use of an integrated developer environment (IDE) or an IDE like configuration such as the IDE RStudio ( or the Nvim-R plug-in for the integration of vim/neovim and R ( jalvesaq/Nvim-R/tree/stable) is recommended but not necessary.

The tutorial was written with RMarkdown (v. 2.6) (Allaire et al., 2020; Xie et al., 2018, 2020) in R (v. 4.0.2) (R Core Team, 2020).

Required R packages:

  • dplyr (Wickham et al., 2020)
  • openxlsx (Schauberger & Walker, 2020)
  • RColorBrewer (Neuwirth, 2014)
  • sf (Pebesma, 2018)
  • tmap (Tennekes, 2018)
  • tidyr (Wickham et al., 2020)


Datatasets used are archived in a zip compressed file ( that can be downloaded at SpatialAnalysisData. The link will connect you to a cloud storage service (MEGA, and ask you to download the file. By accessing the cloud storage service and downloading the file/data you agree to the terms of service of MEGA and to the terms of use of the code and data.


Allaire, J., Xie, Y., McPherson, J., Luraschi, J., Ushey, K., Atkins, A., Wickham, H., Cheng, J., Chang, W., & Iannone, R. (2020). rmarkdown: Dynamic Documents for R.

Neuwirth, E. (2014). RColorBrewer: ColorBrewer Palettes

Pebesma, E. (2018). Simple Features for R: Standardized Support for Spatial Vector Data. The R Journal, 10(1), 439–446. doi: 10.32614/RJ-2018-009

R Core Team. (2020). R: A Language and Environment for Statistical Computing. R Foundation for Statistical Computing.

Schauberger, P., & Walker, A. (2020). openxlsx: Read, Write and Edit xlsx Files.

Tennekes, M. (2018). tmap: Thematic maps in R. Journal of Statistical Software84(6), 1–39. doi: 10.18637/ jss.v084.i06

Wickham, H., François, R., Henry, L., & Müller, K. (2020). dplyr: A Grammar of Data Manipulation.
Wickham, H., & Henry, L. (2020). tidyr: Tidy Messy Data
Xie, Y., Allaire, J. J., & Grolemund, G. (2018). R Markdown: The definitive guide. Chapman; Hall/CRC.
Xie, Y., Dervieux, C., & Riederer, E. (2020). R Markdown Cookbook. Chapman; Hall/CRC.



Cover image:  Land Use in Greater Baltimore (2002).
Data source:  Maryland Archived Land Use Land Cover 2002, Maryland Department of Planning,  online at
.  Accessed on April 17, 2021.


Icon for the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License

Spatial Analysis and Mapping with R: A Short Tutorial Copyright © 2021 by Wolf T. Pecher is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License, except where otherwise noted.