Spatial data in R

On using R as a geographic information system.

I am indebted to Ross Darnell and Petra Kuhnert for invaluable tips that got my understanding started.

Theory of spatial statistics

Not R-specific. See spatial statistics or spatiotemporal statistics.

How to

Robin Lovelace, Jakub Nowosad and Jannes Muenchow’s free textbook, Geocomputation with R, seems to be the most up-to-date resource. Slightly more recently updated but with less promotion, Edzer Pebesma and Roger Bivand’s Spatial Data Science seems like it might be good. Manuel Gimond’s lecture notes are good: Intro to GIS and Spatial Analysis.


For theory see spatial statistics.

Technical details go here.

There are some standards to be aware of. . The classic R format for spatial data information is sp, which is some kind of convention for encoding spatial data in something similar to, but not totally like, a dataframe. Much spatial code in R uses sp-style access.

More recently, sf has become the hotness. This is several things. Firstly there is a standard (Simple features / simple feature access) which is nothing to do with R. This has a formal ISO standard number and reams of documentation about how computers can handle the spatial geometry and informatics. This standard now underlies ESRI ArcGIS), GDAL and the GeoJSON standard.

The sf is also an R package which plugs various technology into all those systems via the sf standard, and aims at superseding sp in the long term.

For now, you need to know both standards exist and convert between them accordingly.

Conversion is apparently simple according to the sf manual

as_Spatial() allows to convert sf and sfc to Spatial*DataFrame and Spatial* for sp compatibility. You can also use as(x, "Spatial") To transform sp objects to sf and sfc with as(x, "sf").

Did you follow that?


R will happily plot a map if you give it some mapping polygons. However, often you want something with more of the affordances of modern interactive web maps. Fancy map tiles with streets and terrain and such are available from map providers such as google maps or stamen. But how to use them in R? Many options.


tmap (“thematic maps”) is one option (manual). It aims to be ggplot-like. It supports a leaflet backend, as does the leaflet library, below, although I think through separate codebases.


ggmap (see quickstart) gets some of the bits and pieces from google maps via mapsapi. rgooglemaps is another alternative, I think. See Laura Ellis’s excellent tutorial, Kahle and Wickham’s tutorial.


leafletR interfaces with leaflet.js. It is independently useful to have also because it has a GeoJSON export function, toGeoJSON which is small, light and fast via the ogre service. For complicated or big projects it falls back to rgdal.

plotly maps

Plotly, does not specialise in maps but if you needs skew more towards the data and less towards the geography, its mapping facilities are adequate, and it has native R integration. See, for example the R bubble maps example.

geojson output

Or, don’t generate the javascript map in R. I can also process data in R then export it as GeoJSON for use in a javascript app/website via GeoJSON or naked GDAL.

GDAL library dependencies

rgdal is an interface to the classic geodata infrastructure GDAL.

rgdal is notoriously fiddly to install and has many system dependencies, but it can do everything, I think. On an ubuntu system, I needed to install devtools and get the packages installed by forcing the tippy-top latest version to install:

install.packages("rgeos", repos="", type="source")
install.packages("rgdal", repos="", type="source")


Baddeley, Adrian, Ege Rubak, and Rolf Turner. 2016. Spatial Point Patterns: Methodology and Applications with R. Champan & Hall/CRC Interdisciplinary Statistics Series. Boca Raton ; London ; New York: CRC Press, Taylor & Francis Group.
Lovelace, Robin, Jakub Nowosad, and Jannes MĂĽnchow. 2019. Geocomputation with R. Boca Raton: Taylor & Francis.
Scheider, Simon, Benedikt Gräler, Edzer Pebesma, and Christoph Stasch. 2016. “Modeling Spatiotemporal Information Generation.” International Journal of Geographical Information Science 30 (10): 1980–2008.
Stasch, Christoph, Simon Scheider, Edzer Pebesma, and Werner Kuhn. 2014. “Meaningful Spatial Prediction and Aggregation.” Environmental Modelling & Software 51 (January): 149–65.

Warning! Experimental comments system! If is does not work for you, let me know via the contact form.

No comments yet!

GitHub-flavored Markdown & a sane subset of HTML is supported.