Skip to main content

12. Spatial vectors in R

Packages

library(terra)      # Reading and manipulating raster data
library(sf)         # Reading and manipulating vector data
library(mapview)    # Map visualization
library(ggplot2)    # Visualization
library(dplyr)      # Manipulate data.frames

Data

Meteorological data

In this exercise, we use long-term average climate station data from the German Meteorological Service (DWD, Climate Data Center). The DWD provides monthly, annual, and multi-annual meteorological data for different time periods. Our dataset contains multi-annual mean precipitation (mm) for each month and for the entire year (column year) for the period 1981–2010. Station locations are given as geographic coordinates: longitude (x) and latitude (y) in WGS 84 (EPSG:4326).

precp_df <- read.csv('data/nieder_1981-2010_aktStandort.csv')
head(precp_df[, -10:-15])
    station_name station_id elevation  y    x               state jan feb mar oct nov dec year
1 AACHEN-ORSBACH      15000       231 51  6.0 Nordrhein-Westfalen  78  72  72  74  73  84  914
2       ABBENSEN         12        61 52 10.2       Niedersachsen  60  46  56  53  56  64  697
3         AFFING         37       470 48 11.0              Bayern  46  42  54  56  54  59  817
4    AHAUS(AWST)       7374        46 52  6.9 Nordrhein-Westfalen  78  61  70  71  76  81  852
5        AHAUSEN         39        29 53  9.3       Niedersachsen  72  54  64  67  66  73  788
6     AHLEN I_W_         42        70 52  7.9 Nordrhein-Westfalen  68  52  64  64  66  70  770

DEM

In addition, we use a digital elevation model (EU-DEM v1.1) from the Copernicus Land Monitoring Service, resampled to a resolution of 1 km × 1 km. Its coordinate reference system is the Lambert Azimuthal Equal Area projection for Europe (ETRS89-LAEA, EPSG:3035).

dem <- rast('data/eu_dem_v11_de_1km.tif')
plot(dem)

Country boundary

To make the output maps easier to interpret, we add the boundaries of selected European countries, stored in a GeoPackage (data/cntry_boundary_europe-subset.gpkg). The boundaries use the same coordinate reference system as the DEM (ETRS89-LAEA, EPSG:3035).

The sf package

The sf package provides classes and functions to read, write, and manipulate vector data in R (Pebesma, 2018; Pebesma and Bivand, 2023). It supersedes the older sp package, which some packages still depend on. The letters sf stand for simple features, a formal standard that makes handling and converting vector data easier.

The sf package represents simple features as native R objects, using simple data structures (S3 classes, lists, matrices, vectors). All functions and methods in sf that operate on spatial data are prefixed with st_, which stands for spatial type.

The sf package uses three classes to represent simple features:

  • sf: a data frame with feature attributes and feature geometries. The geometry column is of class sfc and is typically called geometry or geom, but any name can be used.

  • sfc: the list-column that holds the geometry of each feature (record). It is composed of sfg objects.

  • sfg: the geometry of an individual simple feature, e.g., a point, linestring, multilinestring, polygon, or multipolygon.

For more details, see the sf package vignettes and Lovelace et al. (2025).

Read a spatial vector dataset

The function read_sf() reads a spatial vector dataset into memory as an sf object. It is equivalent to st_read() (recall that sf functions start with st_), but runs quietly and stores the attributes as a tibble.

cntry <- read_sf('data/cntry_boundary_europe-subset.gpkg')
head(cntry)
Simple feature collection with 6 features and 6 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 3600000 ymin: 1900000 xmax: 5800000 ymax: 3800000
Projected CRS: ETRS89-extended / LAEA Europe
# A tibble: 6 × 7
  COUNTRY                ISO_CC CONTINENT Land_Type   Land_Rank COUNTRYAFF                      geom
  <chr>                  <chr>  <chr>     <chr>           <int> <chr>             <MULTIPOLYGON [m]>
1 Albania                AL     Europe    Primary la…         5 Albania    (((5150486 2214643, 5156…
2 Andorra                AD     Europe    Primary la…         5 Andorra    (((3640340 2192709, 3638…
3 Austria                AT     Europe    Primary la…         5 Austria    (((4832066 2857840, 4832…
4 Belarus                BY     Europe    Primary la…         5 Belarus    (((5787402 3444248, 5784…
5 Belgium                BE     Europe    Primary la…         5 Belgium    (((4e+06 3079060, 4e+06 …
6 Bosnia and Herzegovina BA     Europe    Primary la…         5 Bosnia an… (((5e+06 2459212, 5e+06 …

The country dataset is a multipolygon vector dataset with the attributes: COUNTRY, ISO_CC, CONTINENT, Land_Type and geom (the geometry column). The geometry column is of class sfc.

class(cntry$geom)
[1] "sfc_MULTIPOLYGON" "sfc"             

Subset features

A spatial data frame acts in many ways like an ordinary data frame. We can subset feature sets by using the square bracket notation. Below, we subset the feature set to only include the boundary of Germany (rows) and the feature attribute ISO_CC (columns).

cntry_de <- cntry[cntry$COUNTRY=="Germany", "ISO_CC"]

Similarly, the dplyr functions work on sf objects as well.

cntry_de <- cntry |>
  dplyr::filter(COUNTRY=="Germany") |>
  dplyr::select(ISO_CC)

Visualize features

The basic plot() function works with sf objects. If you use plot(), all feature attributes are plotted. For more information, see the sf plotting vignette.

plot(cntry, max.plot=4)

Subset the dataset using square brackets to visualize only one (or selected) feature attributes:

plot(cntry["COUNTRY"])

It is also possible to only plot the geometry.

plot(st_geometry(cntry))

Create a spatial data frame

We can convert our precipitation data frame into a spatial data frame (sf object) using the function st_as_sf(). To define the coordinate reference system (crs), we use the EPSG code of the geographic coordinate system WGS 84 (EPSG:4326). The coordinates are supplied via the function argument coords, either as a vector of the names of the columns that contain the coordinates, or as a matrix that contains the coordinates. In our case, the precipitation data frame contains the columns x and y.

precp_geo <- st_as_sf(precp_df, coords=c('x','y'), crs=4326)
precp_geo[, -9:-16]
Simple feature collection with 874 features and 9 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 6 ymin: 47 xmax: 15 ymax: 55
Geodetic CRS:  WGS 84
First 10 features:
      station_name station_id elevation               state jan feb mar apr year       geometry
1   AACHEN-ORSBACH      15000       231 Nordrhein-Westfalen  78  72  72  59  914   POINT (6 51)
2         ABBENSEN         12        61       Niedersachsen  60  46  56  42  697  POINT (10 52)
3           AFFING         37       470              Bayern  46  42  54  54  817  POINT (11 48)
4      AHAUS(AWST)       7374        46 Nordrhein-Westfalen  78  61  70  48  852 POINT (6.9 52)
5          AHAUSEN         39        29       Niedersachsen  72  54  64  44  788 POINT (9.3 53)
6       AHLEN I_W_         42        70 Nordrhein-Westfalen  68  52  64  44  770 POINT (7.9 52)
7  AHORN-EUBIGHEIM         47       338   Baden-Württemberg  64  56  66  51  783 POINT (9.6 50)
8       ALBERSDORF         66        40  Schleswig-Holstein  76  59  66  46  939 POINT (9.3 54)
9  ALBSTADT-BADKAP         71       759   Baden-Württemberg  68  64  79  72 1014   POINT (9 48)
10   ALFELD (AWST)       7367       144       Niedersachsen  83  64  74  54  877 POINT (9.8 52)

Coordinate reference systems

The function st_crs() is used to access or define the coordinate reference system.

st_crs(precp_geo)
Coordinate Reference System:
  User input: EPSG:4326 
  wkt:
GEOGCRS["WGS 84",
    ENSEMBLE["World Geodetic System 1984 ensemble",
        MEMBER["World Geodetic System 1984 (Transit)"],
        MEMBER["World Geodetic System 1984 (G730)"],
        MEMBER["World Geodetic System 1984 (G873)"],
        MEMBER["World Geodetic System 1984 (G1150)"],
        MEMBER["World Geodetic System 1984 (G1674)"],
        MEMBER["World Geodetic System 1984 (G1762)"],
        MEMBER["World Geodetic System 1984 (G2139)"],
        MEMBER["World Geodetic System 1984 (G2296)"],
        ELLIPSOID["WGS 84",6378137,298.257223563,
            LENGTHUNIT["metre",1]],
        ENSEMBLEACCURACY[2.0]],
    PRIMEM["Greenwich",0,
        ANGLEUNIT["degree",0.0174532925199433]],
    CS[ellipsoidal,2],
        AXIS["geodetic latitude (Lat)",north,
            ORDER[1],
            ANGLEUNIT["degree",0.0174532925199433]],
        AXIS["geodetic longitude (Lon)",east,
            ORDER[2],
            ANGLEUNIT["degree",0.0174532925199433]],
    USAGE[
        SCOPE["Horizontal component of 3D system."],
        AREA["World."],
        BBOX[-90,-180,90,180]],
    ID["EPSG",4326]]

We can also (re-)assign a coordinate reference system. Note, this does not reproject the dataset.

st_crs(precp_geo) <- 4326

Using st_transform(), we can reproject the spatial data frame to another coordinate system. Here, we reproject it to the same coordinate reference system as the DEM.

precp_laea <- st_transform(precp_geo, 3035)
st_crs(precp_laea)
Coordinate Reference System:
  User input: EPSG:3035 
  wkt:
PROJCRS["ETRS89-extended / LAEA Europe",
    BASEGEOGCRS["ETRS89",
        ENSEMBLE["European Terrestrial Reference System 1989 ensemble",
            MEMBER["European Terrestrial Reference Frame 1989"],
            MEMBER["European Terrestrial Reference Frame 1990"],
            MEMBER["European Terrestrial Reference Frame 1991"],
            MEMBER["European Terrestrial Reference Frame 1992"],
            MEMBER["European Terrestrial Reference Frame 1993"],
            MEMBER["European Terrestrial Reference Frame 1994"],
            MEMBER["European Terrestrial Reference Frame 1996"],
            MEMBER["European Terrestrial Reference Frame 1997"],
            MEMBER["European Terrestrial Reference Frame 2000"],
            MEMBER["European Terrestrial Reference Frame 2005"],
            MEMBER["European Terrestrial Reference Frame 2014"],
            MEMBER["European Terrestrial Reference Frame 2020"],
            MEMBER["ETRS89-DNK"],
            MEMBER["Estonia 1997"],
            MEMBER["Albanian Geodetic Reference Frame 2008"],
            MEMBER["Albanian Geodetic Reference Frame 2014"],
            MEMBER["ETRS89-AUT [2002]"],
            MEMBER["Belgian Reference Frame 2002"],
            MEMBER["Belgian Reference Frame 2011"],
            MEMBER["BH_ETRS89"],
            MEMBER["Bulgaria Geodetic System 2005"],
            MEMBER["Croatian Terrestrial Reference System 1996"],
            MEMBER["AGRS2010 (ETRF2000)"],
            MEMBER["CROPOS"],
            MEMBER["ETRF2000 Poland"],
            MEMBER["ETRS89/DREF91 Realization 2016"],
            MEMBER["ETRS89/DREF91 Realization 2025"],
            MEMBER["ETRS89-CZE [2007]"],
            MEMBER["ETRS89-ESP [REGENTE]"],
            MEMBER["ETRS89-ESP [ERGNSS]"],
            MEMBER["ETRS89-FRO [2008]"],
            MEMBER["Hellenic Terrestrial Reference System of 2007"],
            MEMBER["ETRS89-HUN [ETRF2000]"],
            MEMBER["ETRS89-IRE [ETRF2000]"],
            MEMBER["ETRS89-MKD [EUREF-MAK2010]"],
            MEMBER["EUREF89"],
            MEMBER["ETRS89-PRT [1995]"],
            MEMBER["ETRS89-ROU [ETRF2000]"],
            MEMBER["ETRS89-SVK [SKTRF09]"],
            MEMBER["ETRS89-SVK [SKTRF2022]"],
            MEMBER["EUREF-FIN"],
            MEMBER["Istituto Geografico Militare 1995"],
            MEMBER["Kosovo Reference System 2001"],
            MEMBER["Latvian geodetic coordinate system 1992"],
            MEMBER["Latvian coordinate system 2020"],
            MEMBER["Lithuania 1994 (ETRS89)"],
            MEMBER["MOLDREF99"],
            MEMBER["OSNet v2009"],
            MEMBER["Reseau Geodesique Francais 1993 v1"],
            MEMBER["Reseau Geodesique Francais 1993 v2"],
            MEMBER["Reseau Geodesique Francais 1993 v2b"],
            MEMBER["Rete Dinamica Nazionale 2008"],
            MEMBER["Serbian Reference Network 1998"],
            MEMBER["Serbian Spatial Reference System 2000"],
            MEMBER["Slovenia Geodetic Datum 1996"],
            MEMBER["SWEREF 99"],
            MEMBER["ETRS89-LUX [2020]"],
            MEMBER["Nordic Geodetic Commission ETRF14"],
            MEMBER["ETRS89-LUX [2025]"],
            MEMBER["Swiss Terrestrial Reference System 1995"],
            ELLIPSOID["GRS 1980",6378137,298.257222101,
                LENGTHUNIT["metre",1]],
            ENSEMBLEACCURACY[0.1]],
        PRIMEM["Greenwich",0,
            ANGLEUNIT["degree",0.0174532925199433]],
        ID["EPSG",4258]],
    CONVERSION["Europe Equal Area 2001",
        METHOD["Lambert Azimuthal Equal Area",
            ID["EPSG",9820]],
        PARAMETER["Latitude of natural origin",52,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8801]],
        PARAMETER["Longitude of natural origin",10,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8802]],
        PARAMETER["False easting",4321000,
            LENGTHUNIT["metre",1],
            ID["EPSG",8806]],
        PARAMETER["False northing",3210000,
            LENGTHUNIT["metre",1],
            ID["EPSG",8807]]],
    CS[Cartesian,2],
        AXIS["northing (Y)",north,
            ORDER[1],
            LENGTHUNIT["metre",1]],
        AXIS["easting (X)",east,
            ORDER[2],
            LENGTHUNIT["metre",1]],
    USAGE[
        SCOPE["Statistical analysis."],
        AREA["Europe - European Union (EU) countries and candidates. Europe - onshore and offshore: Albania; Andorra; Austria; Belgium; Bosnia and Herzegovina; Bulgaria; Croatia; Cyprus; Czechia; Denmark; Estonia; Faroe Islands; Finland; France; Germany; Gibraltar; Greece; Hungary; Iceland; Ireland; Italy; Kosovo; Latvia; Liechtenstein; Lithuania; Luxembourg; Malta; Monaco; Montenegro; Netherlands; North Macedonia; Norway including Svalbard and Jan Mayen; Poland; Portugal including Madeira and Azores; Romania; San Marino; Serbia; Slovakia; Slovenia; Spain including Canary Islands; Sweden; Switzerland; Türkiye (Turkey); United Kingdom (UK) including Channel Islands and Isle of Man; Vatican City State."],
        BBOX[24.6,-35.58,84.73,44.83]],
    ID["EPSG",3035]]

sf and ggplot

The sf package also provides functions like geom_sf() to work with ggplot(). In the map below, we display the annual precipitation, which is stored in the feature attribute year. The map is drawn in the coordinate reference system of the first layer (here EPSG:3035), but the graticule is labeled in longitude/latitude by default. You can change this with coord_sf(datum = ...).

ggplot() +
  geom_sf(data=precp_laea, aes(col=year)) +
  geom_sf(data=st_geometry(cntry_de), fill=NA) +
  theme_light() # + coord_sf(datum = st_crs(3035))

Interactive visualization

The mapview package provides a convenient way to visualize geodata interactively on top of different base maps such as OpenStreetMap. The default base map (CARTO) now requires an API key, so we select other base maps with mapviewOptions().

mapviewOptions(fgb = FALSE,
               basemaps = c("OpenStreetMap", "Esri.WorldImagery", "OpenTopoMap"))
mapview(precp_laea["year"])

Extract raster values

We can extract raster attributes for vector features using the extract() function from the terra package. Here, we extract elevation from the Copernicus DEM at every meteorological station. Note, the precipitation data already contains an elevation attribute. You can compare the two values if you like.

elev1k <- terra::extract(dem, precp_laea)
head(elev1k)
  ID eu_dem_v11_de_1km
1  1               205
2  2                64
3  3               484
4  4                44
5  5                21
6  6                76

Add extracted values to the precipitation data frame.

precp_laea$elev1k <- elev1k[,2] # remove ID column
head(precp_laea)
Simple feature collection with 6 features and 18 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 4e+06 ymin: 2800000 xmax: 4400000 ymax: 3300000
Projected CRS: ETRS89-extended / LAEA Europe
    station_name station_id elevation               state jan feb mar apr mai jun jul aug sept oct
1 AACHEN-ORSBACH      15000       231 Nordrhein-Westfalen  78  72  72  59  77  89  80  82   74  74
2       ABBENSEN         12        61       Niedersachsen  60  46  56  42  55  67  68  73   57  53
3         AFFING         37       470              Bayern  46  42  54  54  90  92 106  96   68  56
4    AHAUS(AWST)       7374        46 Nordrhein-Westfalen  78  61  70  48  65  74  77  76   75  71
5        AHAUSEN         39        29       Niedersachsen  72  54  64  44  58  70  80  73   67  67
6     AHLEN I_W_         42        70 Nordrhein-Westfalen  68  52  64  44  63  65  71  69   74  64
  nov dec year                geometry elev1k
1  73  84  914   POINT (4e+06 3084134)    205
2  56  64  697 POINT (4333483 3252669)     64
3  54  59  817 POINT (4393745 2817509)    484
4  76  81  852 POINT (4110850 3223695)     44
5  66  73  788 POINT (4275191 3328909)     21
6  66  70  770 POINT (4173751 3186191)     76

Rasterize vectors

The rasterize() function of the terra package can be used to turn spatial vectors into rasters. When rasterizing point vectors, a function is applied to all points falling into a grid cell. For example, the function length() counts the number of points in a grid cell.

The rasterize() function requires a template raster as input. The template raster can be an existing, valid raster or an empty raster (no cell values). Here, we use the DEM raster as template, but change the spatial resolution to 20 km × 20 km. Note, we lose the pixel values, when we simply change the spatial resolution like that. But that is OK for a template.

template <- dem
res(template) <- c(20000, 20000)

The rasterize() function transfers values from vector data to the cells of a template raster. The argument field names the attribute to transfer, and fun defines how values are summarized when several features fall into the same cell. The argument fun takes either the name of a built-in function or a function object. Let’s create two rasters: countgrid contains the number of stations in each grid cell and meangrid contains the mean annual precipitation (field=year).

countgrid <- terra::rasterize(precp_laea,
                              template,
                              fun = "length",
                              background = NA)
meangrid <- terra::rasterize(precp_laea,
                             template,
                             field="year",
                             fun = "mean",
                             background = NA)

To plot both rasters side by side, we combine them with c(). To draw the country boundary on each panel, we pass the plotting command as a function to the fun argument of plot().

plot(c(countgrid, meangrid),
     main=c("Count", "Mean annual precipitation"),
     fun=function() plot(st_geometry(cntry_de), add=TRUE))

References

Lovelace, R., Nowosad, J., Münchow, J., 2025. Geocomputation with R, 2nd ed. Chapman & Hall/CRC, Boca Raton. https://doi.org/10.1201/9780203730058
Pebesma, E., 2018. Simple features for R: Standardized support for spatial vector data. The R Journal 10, 439–446. https://doi.org/10.32614/RJ-2018-009
Pebesma, E., Bivand, R., 2023. Spatial data science: With applications in R. Chapman and Hall/CRC. https://doi.org/10.1201/9780429459016