Base R includes many functions that can be used for reading, visualising, and analysing spatial data. The focus in this view is on “geographical” spatial data, where observations can be identified with geographical locations, and where additional information about these locations may be retrieved if the location is recorded with care. (View Highlight)
The contributed packages address two broad areas: moving spatial data into and out of R including coordinate transformation, and analysing spatial data in R. Because the contributed packages constitute an evolving ecosystem, there are several points of entry for users looking for help and information. Two informal organisations curate websites: r-spatial with a hyphen, and rspatial without. R-spatial is more generally geo-informatics based, grew from the legacy sp package and is now clearly aligned with the modern sf and stars packages. Rspatial has grown from the raster package, now moving towards the modern terra package. It is also worth noting the wealth of online book projects, which may be helpful for users seeking an introduction, including Geocomputation with R. (View Highlight)
Spatial data - general
• sf is a CRAN package for spatial vector data, providing Simple Features for R, in compliance with the OGC Simple Feature standard. The development of the package was supported by the R Consortium. It provides simple features access for vector data, and as such is a modern implementation and standardization of parts of the legacy sp package. sf is documented in an R Journal article. sf uses the PROJ, GEOS and GDAL external software libraries, which must be available for source installs together with other external software libraries that they in turn depend on.
• stars is being actively developed and was initially supported by the R Consortium; it provides for spatiotemporal data in the form of dense arrays. It supercedes the spacetime package, which extended the shared classes defined in sp for spatio-temporal data (see Spatio-Temporal Data in R). stars uses PROJ and GDAL through sf.
• The vapour and gdalraster packages offer low-level access to GDAL functionality for R packages.
• The spatstat contains classes suited to the analysis of point patterns, and may be coerced to and from "sf", "stars" and other spatial classes. (View Highlight)
Raster data
• terra is a re-implementation of raster functionality, linking directly to PROJ, GDAL and GEOS, and introducing new S4 classes for raster and vector data. See the manual and tutorials to get started. terra is very similar to the raster package; but terra is simpler, better, and faster.
• stars provides for spatiotemporal data in the form of dense arrays, with space and time being array dimensions. Examples include socio-economic or demographic data, environmental variables monitored at fixed stations, time series of satellite images with multiple spectral bands, spatial simulations, and climate model results.
• The gdalcubes package also provides classes for data cubes, including proxy data cubes, it links to PROJ, GDAL and NetCDF4. (View Highlight)
Geographic metadata
• geometa provides classes and methods to write geographic metadata following the ISO and OGC metadata standards (ISO 19115, 19110, 19119) and export it as XML (ISO 19139) for later publication into metadata catalogues. Reversely, geometa provides a way to read ISO 19139 metadata into R. The package extends sf to provide GML (ISO 19136) representation of geometries.
• ncdf4 provides read and write functions for handling metadata (CF conventions) in the self-described NetCDF format.
• CFtime encapsulates the CF time coordinate and allows to deal with the different CF calendars (View Highlight)
Spatial sampling
• spsurvey provides functions to select generalized random-tessellation stratified (GRTS) probability samples and analyze survey data.
• Spbsampling allows selecting probability samples well spread over the population of interest, in any dimension and using any distance function.
• spatialsample is a member of the tidymodels family of packages and contains functions and classes for spatial resampling to use with rsample.
• MBHdesign provides spatially survey balanced designs using the quasi-random number method. (View Highlight)
Base visualization packages
• Packages such as sf, stars, terra, and rasterVis provide basic visualization methods through the generic plot function.
• classInt package provides functions for choosing class intervals for thematic cartography.
• Currently, the grDevices package (included with the R installation) contains a large number of color palettes that can be accessed with the hcl.colors and palette.colors functions; see also New features in this blog. Some of these color palettes can be also retrieved using separate packages, such as RColorBrewer, viridis, or rcartocolor. (View Highlight)
Thematic cartography packages
• tmap package accepts most spatial data classes and provides a modern basis for thematic mapping using a Grammar of Graphics syntax. It also allows for interactive spatial data mapping.
• mapsf package allows various cartographic representations such as proportional symbols, choropleth, or typology maps; it accepts sf (sf) and SpatRaster (terra) objects
• ggplot2 package has a built-in support for sf objects with the geom_sf function and additional support for stars object is available through the geom_stars function available in the stars package. Its spatial visualization capabilities can be further extended with ggspatial, which adds support for more spatial classes (including classes from the raster package), allows adding north arrows and scale bars, etc.
• The mapmisc package is a minimal, light-weight set of tools for producing nice-looking maps in R, with support for map projections.
• Additional processing and mapping functions are available in PBSmapping package; PBSmodelling provides modelling support. In addition, GEOmap provides mapping facilities directed to meet the needs of geologists and uses the geomapdata package. (View Highlight)
Packages based on web-mapping frameworks
• mapview and leaflet packages provide methods to view spatial objects interactively, usually on a web mapping base. Additionally, tmap has a view mode that allows for interactive spatial data mapping.
• mapdeck package provides a mechanism to plot interactive maps through javascript libraries ‘Mapbox GL’ and ‘Deck.gl’.
• RgoogleMaps package for accessing Google Maps(TM) may be useful if the user wishes to place a map backdrop behind other displays.
• ggmap may be used for spatial visualization with Google Maps and OpenStreetMap.
• mapedit provides an R shiny widget based on leaflet for editing or creating sf geometries. (View Highlight)
Point pattern analysis
Point pattern analysis examines the distance relationships between observed points, where the set of observations is expected to encompass all such entities in the study area.
• spatstat is a family of R packages for analysing spatial point pattern data (and other kinds of spatial data). It has extensive capabilities for exploratory analysis, statistical modelling, simulation and statistical inference. It allows freedom in defining the region(s) of interest, and makes extensions to marked processes and spatial covariates. Its strengths are model-fitting and simulation, and it has a useful homepage; it is actively developed. It is the only package that will enable the user to fit inhomogeneous point process models with interpoint interactions.
• The splancs package allows point data to be analysed within a polygonal region of interest, and covers many methods, including 2D kernel densities.
• The spatial package is a recommended package shipped with base R, and contains several core functions, including an implementation of Khat by its author, Prof. Ripley.
• The spatgraphs package provides graphs, graph visualisation and graph based summaries to be used with spatial point pattern analysis.
• The smacpod package provides various statistical methods for analyzing case-control point data. The methods available closely follow those in chapter 6 of Applied Spatial Statistics for Public Health Data by Waller and Gotway (2004).
• ecespa provides wrappers, functions and data for spatial point pattern analysis, used in the book on Spatial Ecology of the ECESPA/AEET. The functions for binning points on grids in
• ads may also be of interest. The ads package performs first- and second-order multi-scale analyses derived from Ripley’s K-function.
• The dbmss package allows simple computation of a full set of spatial statistic functions of distance, including classical ones (Ripley’s K and others) and more recent ones used by spatial economists (Duranton and Overman’s Kd, Marcon and Puech’s M). It relies on spatstat for core calculation. (View Highlight)
Geostatistics
Geostatistics uses a model fitted using the distances between observations to interpolate values observed at point to unobserved points
• The gstat package provides a wide range of functions for univariate and multivariate geostatistics, also for larger datasets.
• geoR contains functions for model-based geostatistics.
• Variogram diagnostics may be carried out with vardiag.
• Automated interpolation using gstat is available in automap.
• This family of packages is supplemented by intamap with procedures for automated interpolation.
• A similar wide range of functions is to be found in the fields package, extended by LatticeKrig for large spatial datasets and autoFRK.
• The spatial package is shipped with base R, and contains several core geostatistical functions.
• The spBayes package fits Gaussian univariate and multivariate models with MCMC.
• ramps is a different Bayesian geostatistical modelling package.
• The geospt package contains some geostatistical and radial basis functions, including prediction and cross validation. Besides, it includes functions for the design of optimal spatial sampling networks based on geostatistical modelling.
• The FRK package is a tool for spatial/spatio-temporal modelling and prediction with large datasets. The approach, discussed in Cressie and Johannesson (2008), decomposes the field, and hence the covariance function, using a fixed set of n basis functions, where n is typically much smaller than the number of data points (or polygons) m.
• SpatialExtremes proposes several approaches for spatial extremes.
• In addition, constrainedKriging and geospt provide alternative approaches to geostatistical modelling.
• The spTimer package is able to fit, spatially predict and temporally forecast large amounts of space-time data using [1] Bayesian Gaussian Process (GP) Models, [2] Bayesian Auto-Regressive (AR) Models, and [3] Bayesian Gaussian Predictive Processes (GPP) based AR Models.
• The rtop package provides functions for the geostatistical interpolation of data with irregular spatial support such as runoff related data or data from administrative units.
• The georob package provides functions for fitting linear models with spatially correlated errors by robust and Gaussian Restricted Maximum Likelihood and for computing robust and customary point and block kriging predictions, along with utility functions for cross-validation and for unbiased back-transformation of kriging predictions of log-transformed data.
• The SpatialTools package has an emphasis on kriging, and provides functions for prediction and simulation. It is extended by ExceedanceTools, which provides tools for constructing confidence regions for exceedance regions and contour lines.
• The gear package implements common geostatistical methods in a clean, straightforward, efficient manner, and is said to be a quasi reboot of SpatialTools.
• The sperrorest package implements spatial error estimation and permutation-based spatial variable importance using different spatial cross-validation and spatial block bootstrap methods, used by mlr3spatiotempcv.
• The sgeostat package is also available. Within the same general topical area are the deldir package for triangulation and the interp package for spline interpolation; the MBA package provides scattered data interpolation with multilevel B-splines.
• In addition, there are the spatialCovariance package, which supports the computation of spatial covariance matrices for data on rectangles, the regress package building in part on spatialCovariance, and the tgp package.
• The archived Stem package provided for the estimation of the parameters of a spatio-temporal model using the EM algorithm, and the estimation of the parameter standard errors using a spatio-temporal parametric bootstrap.
• The SSN2 is for geostatistical modeling for data on stream networks, including models based on in-stream distance. Models are created using moving average constructions. Spatial linear models, including covariates, can be fit with ML or REML. Mapping and other graphical functions are supported.
• The ipdw provides functions to interpolate georeferenced point data via Inverse Path Distance Weighting. Useful for coastal marine applications where barriers in the landscape preclude interpolation with Euclidean distances.
• sptotal uses Finite Population Block Kriging (FPBK) to provide a prediction for a quantity of interest, most commonly a population total or a prediction of total abundance, on a finite number of spatial sites.
• spmodel fits statistical models to geostatistical and areal spatial data using a variety of covariance structures. Additional functionality allows for prediction (Kriging), non-spatial random effects, anisotropy, and big data. (View Highlight)
Spatial regression
Many packages providing functions for fitting spatial regression models have already been given as they are used in disease mapping. In this subsection, more attention is given to the subset of methods used in spatial econometrics, and so complements general econometric methods covered in the Econometrics Task View.
• The choice of function for spatial regression will depend on the support available. If the data are characterised by point support and the spatial process is continuous, geostatistical methods may be used, or functions in the nlme package.
• If the support is areal, and the spatial process is not being treated as continuous, functions provided in the spatialreg package may be used. This package can also be seen as providing spatial econometrics functions. spdep provides the full range of local indicators of spatial association, such as local Moran’s I and diagnostic tools for fitted linear models, including Lagrange Multiplier tests. Spatial regression models that can be fitted using maximum likelihood and Bayesian MCMC methods in spatialreg include spatial lag models, spatial error models, two parameter models, their Durbin variants and SLX models. For larger data sets, sparse matrix techniques can be used for maximum likelihood fits. In spatialreg, the ME and SpatialFiltering functions provide Moran Eigenvector model fitting, as do more modern functions in the spmoran package.
• When using the generalized method of moments (GMM), sphet can be used to accommodate both autocorrelation and heteroskedasticity, also with instrumental variables.
• The splm package provides methods for fitting spatial panel data by maximum likelihood and GM.
• The spsur package provides functions to test and estimate spatial seemingly unrelated regression models (spatial SUR) by maximum likelihood and three-stage least squares.
• The two small archived packages S2sls and spanel provide alternative implementations without most of the facilities of splm.
• The former HSAR package provides Hierarchical Spatial Autoregressive Models (HSAR), based on a Bayesian Markov Chain Monte Carlo (MCMC) algorithm.
• spatialprobit makes possible Bayesian estimation of the spatial autoregressive probit model (SAR probit model).
• The ProbitSpatial package provides methods for fitting Binomial spatial probit models to larger data sets; spatial autoregressive (SAR) and spatial error (SEM) probit models are included.
• The starma package provides functions to identify, estimate and diagnose a Space-Time AutoRegressive Moving Average (STARMA) model.
• varycoef and spBayes provide implementations of spatially varying coefficient (SVC) models, which may be preferred to geographically weighted regression (GWR) models as having proper statistical foundations.
• The gwrr package fits geographically weighted regression (GWR) models and has tools to diagnose and remediate collinearity in the GWR models. It also fits geographically weighted ridge regression (GWRR) and geographically weighted lasso (GWL) models. The GWmodel package contains functions for computing geographically weighted (GW) models. Specifically, basic, robust, local ridge, heteroskedastic, mixed, multiscale, generalised and space-time GWR; GW summary statistics, GW PCA and GW discriminant analysis; associated tests and diagnostics; and options for a range of distance metrics.
• waywiser helps assess models fit to spatial data, with functions for calculating the spatial autocorrelation of model residuals, for calculating model performance statistics, for assessing model performance across multiple spatial scales, and for calculating the “area of applicability” of a model. Functions are designed to be compatible with both base R and with the tidymodels modeling framework, and adopt yardstick classes and interfaces. (View Highlight)
Machine learning of spatial data
Machine learning of spatial data requires specialized methods to account for spatial dependencies like spatial autocorrelation – where nearby observations tend to be similar. Ignoring these dependencies during model training and evaluation risks information leakage. For example, randomly splitting spatial data into training and testing subsets, without considering spatial autocorrelation, can result in test samples being spatially close to training samples. This violates the assumption of independence between training and test sets and can lead to inflated performance metrics and poor model generalization.
To address this, various approaches and methods to account for spatial dependencies and relationships when building models were developed. In general, machine learning of spatial data can be performed through one of the existing machine learning frameworks in R, such as caret, mlr3, and tidymodels or through specialized spatial machine learning packages.
• The caret package provides a consistent interface for training models but requires additional packages like blockCV or CAST to implement spatial methodologies. Functions like CAST::knndm and CAST::ffs enable spatially aware feature selection and cross-validation, while CAST::aoa assesses the area of applicability for spatial models.
• mlr3 with mlr3spatial and mlr3spatiotempcv takes an object-oriented approach with R6 classes for direct spatial object handling and cross-validation within its structured syntax.
• tidymodels with spatialsample and waywiser introduces spatial sampling strategies and model evaluation tools following tidyverse principles, including spatialsample::spatial_resample and waywiser::ww_area_of_applicability.
• RandomForestsGLS and spatialRF extend Random Forests to incorporate spatial dependence, offering specialized functions for spatial estimation, feature selection, and model assessment.
• meteo implements Random Forest Spatial Interpolation by incorporating nearest observations and distances into the prediction process.
• gpboost captures complex non-linear dependencies by combining gradient boosting with Gaussian processes.
• sperrorest and blockCV provide frameworks for spatial resampling and validation, supporting methods like k-means clustering and block-based approaches to account for spatial dependencies in model evaluation. (View Highlight)