R Spatial Analysis Skill
Sub-skills
| Sub-skill |
Description |
| r-spatial-vector |
sf, sp, terra vectors, geometric operations |
| r-spatial-raster |
terra, raster, stars, gridded data |
| r-spatial-mapping |
tmap, leaflet, mapview, ggplot2+sf |
Geographic and spatial data analysis in R.
Core Spatial Packages
| Package |
Description |
| sf ★ |
Simple Features for R (modern) |
| terra ★ |
Spatial data analysis (raster/vector) |
| sp |
Classes for spatial data (legacy) |
| rgdal |
Geospatial Data Abstraction Library |
| rgeos |
Geometry Engine - Open Source |
Mapping & Visualization
| Package |
Description |
| leaflet ★ |
Interactive maps |
| tmap ★ |
Thematic maps |
| ggmap |
Maps with ggplot2 |
| maptools |
Spatial object tools |
| RColorBrewer |
Color schemes for maps |
| REmap |
ECharts maps |
Spatial Statistics
| Package |
Description |
| spatstat |
Spatial point pattern analysis |
| gstat |
Geostatistical modeling |
| spdep |
Spatial dependence |
| GWmodel |
Geographically-weighted models |
Spatio-temporal
| Package |
Description |
| spacetime |
Spatio-temporal data |
Data Sources
| Package |
Description |
| tigris |
US Census TIGER shapefiles |
| rnaturalearth |
Natural Earth map data |
| osmdata |
OpenStreetMap data |
Quick Examples
# sf basics
library(sf)
# Read shapefile
shp <- st_read("data.shp")
# Read GeoJSON
geo <- st_read("data.geojson")
# Create from coordinates
points <- st_as_sf(df, coords = c("lon", "lat"), crs = 4326)
# Coordinate reference systems
st_crs(shp)
shp_transformed <- st_transform(shp, crs = 3857)
# Spatial operations
st_intersection(a, b)
st_union(a, b)
st_buffer(points, dist = 1000)
st_area(polygons)
st_distance(a, b)
# Spatial joins
st_join(points, polygons)
# Interactive map with leaflet
library(leaflet)
leaflet(shp) %>%
addTiles() %>%
addPolygons(
fillColor = ~colorQuantile("YlOrRd", value)(value),
weight = 1,
popup = ~name
)
# Static map with tmap
library(tmap)
tm_shape(shp) +
tm_polygons("value",
style = "quantile",
palette = "Blues",
title = "Value"
) +
tm_layout(title = "Map Title")
# ggplot2 + sf
library(ggplot2)
ggplot(shp) +
geom_sf(aes(fill = value)) +
scale_fill_viridis_c() +
theme_minimal()
# Spatial statistics
library(spdep)
# Create neighbors
nb <- poly2nb(shp)
# Spatial weights
lw <- nb2listw(nb)
# Moran's I
moran.test(shp$value, lw)
# Geostatistics
library(gstat)
# Variogram
v <- variogram(value ~ 1, data = points)
plot(v)
# Kriging
v.fit <- fit.variogram(v, vgm("Sph"))
kriged <- krige(value ~ 1, points, grid, v.fit)
Common Workflows
Choropleth Map
library(sf)
library(tmap)
# Load data
shp <- st_read("boundaries.shp")
data <- read.csv("data.csv")
# Join
shp <- merge(shp, data, by = "id")
# Map
tmap_mode("view") # Interactive
tm_shape(shp) +
tm_polygons("value",
style = "jenks",
palette = "RdYlBu",
title = "Value"
) +
tm_borders() +
tm_layout(legend.outside = TRUE)
Point Pattern Analysis
library(sf)
library(spatstat)
# Convert to ppp
window <- as.owin(boundary)
ppp_obj <- as.ppp(st_coordinates(points), W = window)
# Density
density_map <- density(ppp_obj)
plot(density_map)
# K-function
K <- Kest(ppp_obj)
plot(K)
Resources
1---2name: r-spatial3description: R spatial analysis packages. Use for geographic data, maps, GIS operations, and spatial statistics.4metadata:5 source: https://skillmd.com/skills/leolin990405/r-spatial6---78# R Spatial Analysis Skill910## Sub-skills1112| Sub-skill | Description |13|-----------|-------------|14| [r-spatial-vector](r-spatial-vector/SKILL.md) | sf, sp, terra vectors, geometric operations |15| [r-spatial-raster](r-spatial-raster/SKILL.md) | terra, raster, stars, gridded data |16| [r-spatial-mapping](r-spatial-mapping/SKILL.md) | tmap, leaflet, mapview, ggplot2+sf |1718Geographic and spatial data analysis in R.1920## Core Spatial Packages2122| Package | Description |23|---------|-------------|24| **sf** ★ | Simple Features for R (modern) |25| **terra** ★ | Spatial data analysis (raster/vector) |26| **sp** | Classes for spatial data (legacy) |27| **rgdal** | Geospatial Data Abstraction Library |28| **rgeos** | Geometry Engine - Open Source |2930## Mapping & Visualization3132| Package | Description |33|---------|-------------|34| **leaflet** ★ | Interactive maps |35| **tmap** ★ | Thematic maps |36| **ggmap** | Maps with ggplot2 |37| **maptools** | Spatial object tools |38| **RColorBrewer** | Color schemes for maps |39| **REmap** | ECharts maps |4041## Spatial Statistics4243| Package | Description |44|---------|-------------|45| **spatstat** | Spatial point pattern analysis |46| **gstat** | Geostatistical modeling |47| **spdep** | Spatial dependence |48| **GWmodel** | Geographically-weighted models |4950## Spatio-temporal5152| Package | Description |53|---------|-------------|54| **spacetime** | Spatio-temporal data |5556## Data Sources5758| Package | Description |59|---------|-------------|60| **tigris** | US Census TIGER shapefiles |61| **rnaturalearth** | Natural Earth map data |62| **osmdata** | OpenStreetMap data |6364## Quick Examples6566```r67# sf basics68library(sf)6970# Read shapefile71shp <- st_read("data.shp")7273# Read GeoJSON74geo <- st_read("data.geojson")7576# Create from coordinates77points <- st_as_sf(df, coords = c("lon", "lat"), crs = 4326)7879# Coordinate reference systems80st_crs(shp)81shp_transformed <- st_transform(shp, crs = 3857)8283# Spatial operations84st_intersection(a, b)85st_union(a, b)86st_buffer(points, dist = 1000)87st_area(polygons)88st_distance(a, b)8990# Spatial joins91st_join(points, polygons)9293# Interactive map with leaflet94library(leaflet)95leaflet(shp) %>%96 addTiles() %>%97 addPolygons(98 fillColor = ~colorQuantile("YlOrRd", value)(value),99 weight = 1,100 popup = ~name101 )102103# Static map with tmap104library(tmap)105tm_shape(shp) +106 tm_polygons("value",107 style = "quantile",108 palette = "Blues",109 title = "Value"110 ) +111 tm_layout(title = "Map Title")112113# ggplot2 + sf114library(ggplot2)115ggplot(shp) +116 geom_sf(aes(fill = value)) +117 scale_fill_viridis_c() +118 theme_minimal()119120# Spatial statistics121library(spdep)122# Create neighbors123nb <- poly2nb(shp)124# Spatial weights125lw <- nb2listw(nb)126# Moran's I127moran.test(shp$value, lw)128129# Geostatistics130library(gstat)131# Variogram132v <- variogram(value ~ 1, data = points)133plot(v)134# Kriging135v.fit <- fit.variogram(v, vgm("Sph"))136kriged <- krige(value ~ 1, points, grid, v.fit)137```138139## Common Workflows140141### Choropleth Map142```r143library(sf)144library(tmap)145146# Load data147shp <- st_read("boundaries.shp")148data <- read.csv("data.csv")149150# Join151shp <- merge(shp, data, by = "id")152153# Map154tmap_mode("view") # Interactive155tm_shape(shp) +156 tm_polygons("value",157 style = "jenks",158 palette = "RdYlBu",159 title = "Value"160 ) +161 tm_borders() +162 tm_layout(legend.outside = TRUE)163```164165### Point Pattern Analysis166```r167library(sf)168library(spatstat)169170# Convert to ppp171window <- as.owin(boundary)172ppp_obj <- as.ppp(st_coordinates(points), W = window)173174# Density175density_map <- density(ppp_obj)176plot(density_map)177178# K-function179K <- Kest(ppp_obj)180plot(K)181```182183## Resources184185- sf: https://r-spatial.github.io/sf/186- terra: https://rspatial.org/187- tmap: https://r-tmap.github.io/tmap/188- leaflet: https://rstudio.github.io/leaflet/189- Geocomputation with R: https://geocompr.robinlovelace.net/