# install
install.packages("sf") # spatial data
install.packages("tidyverse") # cleaning dataIntroduction to Spatial Data in R
Introduction
Spatial data are data that contain information about the location and shape of observations. Unlike ordinary tabular data (e.g., dataframes), which primarily describe what an observation is, spatial data also describe where an observation is located.
Spatial data are used across a wide range of disciplines and applications. Researchers may use spatial data to map demographic characteristics, study patterns of land use, analyze access to services, examine environmental conditions, or investigate how observations are distributed across space. In these applications, geographic location is an important part of the data itself.
Spatial data are commonly divided into two broad types: vector and raster
Vector data represent discrete geographic features using points, lines, and polygons. For example, points can represent individual locations, lines can represent roads or rivers, and polygons can represent areas such as neighborhoods or administrative boundaries.
Raster data represent geographic space as a grid of cells (pixels), with each cell containing a value. Raster data are commonly used to represent continuous phenomena such as elevation, temperature, or land cover.
In this vignette, we focus on vector spatial data and introduce the tools and concepts needed to work with them in R.
R Packages
You will need the following R packages. If you have already installed them, skip the install.packages lines.
# load
library(sf)
library(tidyverse)The primary package for working with vector spatial data in R is sf, developed by Pebesma & Bivand (2023) and Pebesma (2018). sf (which stands for “simple features”) provides a consistent framework for storing, manipulating, mapping, and analyzing spatial data in R. Importantly, sf objects retain the structure of a regular dataframe while adding a geometry column that describes the location and shape of each observation.
Spatial Data Types
Spatial data can represent different types of geographic features. Specifically, the geometry column inherent to spatial data contain the coordinate pairs that, in aggregate, create the spatial feature. The three most common types of spatial features are points, lines, and polygons. These different geometry types allow spatial data to describe both individual locations and geographic areas.
Points represent individual locations, such as addresses, schools, hospitals, or bus stops.
Figure 1 displays Link Light Rail stations in King County, Washington. These data are stored as points and reflect the coordinates of individual stations. Scroll through the map and click on a point to see the name of the station!
Table 1 provides 10 of the station observations from Figure 1. These data are drawn directly from the raw spatial data. Notice that each observation contains a single pair of coordinates (i.e., longitude and latitude), reflecting the fact that point features are just that - a single point based on individual pairs of coordinates.
Thus, the geometry column for point features contains just one coordinate pair per observation.
| Station Name | Coordinate Pair |
|---|---|
| Westlake Station | -122.34, 47.61 |
| University Street Station | -122.34, 47.61 |
| Pioneer Square Station | -122.33, 47.6 |
| International District Station | -122.33, 47.6 |
| Stadium Station | -122.33, 47.59 |
| SODO Station | -122.33, 47.58 |
| Beacon Hill Station | -122.31, 47.58 |
| Mount Baker Station | -122.3, 47.58 |
| Columbia City Station | -122.29, 47.56 |
| Othello Station | -122.28, 47.54 |
Lines represent linear features, such as roads, rivers, transit routes, or trails.
Figure 2 displays the Link Light Rail routes associated with the stations in Figure 1. Here, data are stored as lines and reflect the paths followed by the Light Rail system.
Table 2 provides 10 route observations from Figure 2. Unlike the point data in Table 1, each observation here contains many coordinate pairs. Because there are so many, Table 2 reports the number of coordinate pairs rather than the raw longitudes and latitudes. The large number of coordinate pairs reflects the structure of line features: collections of connected points that, together, represent the length and location of a line. Each turn in a Light Rail route is stored in the geometry column as multiple coordinate pairs, which collectively approximate the curve of a line. As expected, longer line features typically contain more coordinate pairs, seen in Table 2 as the positive correlation between Route Length and Coordinate Pairs.
Thus, the geometry column for line features typically contain multiple coordinate pairs per observation.
| Route Name | Route Length (miles) |
Coordinate Pairs |
|---|---|---|
| Central Link | 13.9 | 3,973 |
| Airport Link | 1.7 | 227 |
| Tacoma Link | 1.8 | 411 |
| University Link | 3.2 | 376 |
| Northgate Link | 3.8 | 199 |
| S 200th Extension | 1.7 | 404 |
| Lynnwood Link | 8.5 | 2,343 |
| Northgate Link | 0.5 | 44 |
| Federal Way Link | 7.8 | 1,538 |
| East Link | 6.1 | 1,128 |
Polygons represent areas, such as states, counties, Census tracts, parks, or watersheds. Unlike points and lines, polygons reflect geographic areas with defined boundaries.
Figure 3 displays public parks in King County, Washington. These data are stored as polygons; each polygon reflects the geographic boundary of a single park. The size and shape of each polygon correspond to the actual area occupied by the park. Scroll through the map and click on a park to see its name!