6 R as a GIS
6.1 Overview
Points, lines, polygons and rasters - R can handle them all and more! R is a free software environment for statistical computing and graphics, but its abilities have been extended into many realms through the >20,000 (!) contributed extension packages (also called libraries).
The list of packages can be bewildering, but fortunately some great folks have taken the time to sift through and make some sense of them for different focal topics and created Task Views.
For GIS there are two Task Views of interest:
- Spatial - maintained by Roger Bivand and Jakub Nowosad, and
- SpatioTemporal - maintained by Edzer Pebesma and Roger Bivand
They overlap somewhat, but the former covers spatial data in general, while the latter specifically focuses on data where both location and time of observation are registered, and relevant for the analysis of the data.
Each has an overview page listing packages and highlighting their respective strengths, weaknesses etc., e.g.
](img/spatialTV.png)
Figure 6.1: Screenshot of the “Spatial” Task View at https://cran.r-project.org/
The Spatial Task View focuses on “Analysis of Spatial Data”, with sections on:
- Classes for spatial data and metadata
- Reading and writing spatial data
- Interfaces to Spatial Web-Services
- Handling spatial data
- Visualizing spatial data
- Analyzing spatial data
Task Views also allow easy download and installation of all packages in a Task View using library(ctv) (which you can install with
install.packages("ctv")
). In this case the code you’d need to install the Task View is ctv::install.views("Spatial", coreOnly = TRUE)
. But beware! If you leave out the coreOnly = TRUE
it can take a while to download and install!!! It may take a while even then… You don’t need the whole Task View for my tutorials, so don’t bother downloading it if you’re just working through these.
](img/taskviews.png)
Figure 6.2: Screenshot of the Task View landing page at https://cran.r-project.org/
6.2 Some key R packages
We don’t have time to go through all packages or provide a full history, but here are some notes in brief.
6.2.1 For vector data
(although some of these packages can handle rasters too)
The leading package was sp. It is still active and useful, but is superseded by a newer package sf, which is a modern implementation and standardization of parts of sp. It is highly recommended that you use sf over the older packages. I’ve put this note here purely because Google searches for “How to … in R?” will likely turn up sp code at times.
sf stands for “Simple Features for R”, in compliance with the OGC Simple Feature standard. It is highly efficient, and comes with the advantage that it uses Tidyverse principles and coding styles, e.g. allowing use of the pipe operator (%>%
) and the direct application of library(dplyr) data manipulation and library(ggplot2) visualization functions. I will use sf for the most part in the demonstration material.
Here’s a quick list of the functions available in sf:
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
## [1] [ [[<-
## [3] [<- $<-
## [5] aggregate as.data.frame
## [7] cbind coerce
## [9] dbDataType dbWriteTable
## [11] duplicated identify
## [13] initialize merge
## [15] plot points
## [17] print rbind
## [19] show slotsFromS3
## [21] st_agr st_agr<-
## [23] st_area st_as_s2
## [25] st_as_sf st_as_sfc
## [27] st_bbox st_boundary
## [29] st_break_antimeridian st_buffer
## [31] st_cast st_centroid
## [33] st_collection_extract st_concave_hull
## [35] st_convex_hull st_coordinates
## [37] st_crop st_crs
## [39] st_crs<- st_difference
## [41] st_drop_geometry st_exterior_ring
## [43] st_filter st_geometry
## [45] st_geometry<- st_inscribed_circle
## [47] st_interpolate_aw st_intersection
## [49] st_intersects st_is_valid
## [51] st_is st_join
## [53] st_line_merge st_m_range
## [55] st_make_valid st_minimum_rotated_rectangle
## [57] st_nearest_points st_node
## [59] st_normalize st_point_on_surface
## [61] st_polygonize st_precision
## [63] st_reverse st_sample
## [65] st_segmentize st_set_precision
## [67] st_shift_longitude st_simplify
## [69] st_snap st_sym_difference
## [71] st_transform st_triangulate_constrained
## [73] st_triangulate st_union
## [75] st_voronoi st_wrap_dateline
## [77] st_write st_z_range
## [79] st_zm text
## [81] transform
## see '?methods' for accessing help and source code
This doesn’t tell you how to use them though. To get help with a function in R just type “?” followed by the function name, e.g. ?st_read
, and it’ll take you to the help page.
Of course, you don’t want to have to read every help page to find the function you want! Fortunately, here’s a “cheat sheet” that allows you to find the function you want relatively quickly (once you’re familiar with the syntax etc):
 by Ryan Garnett (page 1).](img/sf1.png)
Figure 6.3: An R cheat sheet for library(sf) by Ryan Garnett (page 1).
 by Ryan Garnett (page 2).](img/sf2.png)
Figure 6.4: An R cheat sheet for library(sf) by Ryan Garnett (page 2).
6.2.2 For raster data
By far the best package has been raster, maintained by Robert Hijmans (of WorldClim fame), and can do just about anything with rasters and interfaces with sp very nicely. Again, as for sp, raster has been superseded by a new package called terra, also developed by Hijmans.
“terra is very similar to the raster package; but terra is simpler, better, and faster” - Roger Bivand
It’s worth noting that terra can handle vector data directly too, and that sometimes its integration with sf is a little clunky, but it does seem to get better all the time. Unfortunately, there’s no cheat sheet for terra, but there’s a lot of documentation and tutorials here.
terra is also able to handle spatiotemporal arrays (raster and vector data cubes). Think of these as time-series of GIS data, like satellite archives, etc. Other powerful packages to watch in the spatiotemporal space are stars and gdalcubes.