Data Analysis and Visualization with R: Spatial
Last updated: March 08, 2025
Prerequisites and Preparations
To get the most out of this spatial section of Data Analysis and Visualization with R, you should have:
- basic knowledge of R/RStudio, generic data processing, and R plots covered in the first two sections.
- a recent version of R and RStudio on your computer.
Recommended Setting-up Steps:
Create a new RStudio project R-spatial in a new folder R-spatial.
Create a new folder under R-spatial and name it data.
If you have your working directory set to R-spatial and it contains a folder called data, you can copy and run the following lines in R:
download.file("http://www.geo.hunter.cuny.edu/~ssun/R-Spatial/data/R-spatial-data.zip",
"R-spatial-data.zip");
unzip("R-spatial-data.zip", exdir = "data")
You can also download the data manually here R-spatial-data.zip and extract the files inside.
- Install and load the following libraries for spatial data handling:
- For spatial data mapping and visualization, install and load these additional libraries:
- You can use the standard methods of
install.packages
andrequire
orlibrary
to install and load these R packages.
Most efficiently, you can run the following script in RStudio to install all the packages that will be used for the spatial section:
# Load a list of packages. Install them first if they are not available.
# The list of packages to be installed
list.of.packages <- c("sf", "sp", "spatial", "maptools", "rgeos","rgdal",
"raster", "grid", "rasterVis",
"tidyverse", "magrittr", "ggpubr", "lubridate",
"devtools", "htmlwidgets", "mapview",
"classInt", "RColorBrewer", "ggmap", "tmap", "leaflet", "mapview",
"ggrepel", "ggsn",
"spdep","spatialreg","GWmodel");
# Check out the packages that have not been installed yet.
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
# Install those missing packages first. It could take a long time for the first time.
if(length(new.packages)>0) install.packages(new.packages)
# Load all packages.
lapply(list.of.packages,function(x) {
require(x,character.only = TRUE,quietly = TRUE)
})
# Or load specific packages when needed
require(sp); require(sf); library(raster)
References
Bivand, RS., Pebesma, E., Gómez-Rubio, V. (2013): Applied Spatial Data Analysis with R
Brunsdon, C. and Comber, L. (2015): An Introduction to R for Spatial Analysis and Mapping
Lovelace, R., Nowosad, J., Muenchow. J. (2019): Geocomputation with R
Spatial Data Analysis and Modeling with R
CRAN Task View: Analysis of Spatial Data
Engel, C. (2019). R for Geospatial Analysis and Mapping. The Geographic Information Science & Technology Body of Knowledge (1st Quarter 2019 Edition), John P. Wilson (Ed.). DOI:10.22224/gistbok/2019.1.3.
Acknowledgements
The materials for this section are adapted from https://cengel.github.io/R-spatial/ developed by Dr Claudia A Engel.