Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

adjacency_matrix

Adjacency matrix


Description

Create a matrix showing which planning units are spatially adjacent to each other. Note that this also include planning units that overlap with each other too.

Usage

adjacency_matrix(x, ...)

## S3 method for class 'Raster'
adjacency_matrix(x, directions = 4L, ...)

## S3 method for class 'SpatialPolygons'
adjacency_matrix(x, ...)

## S3 method for class 'SpatialLines'
adjacency_matrix(x, ...)

## S3 method for class 'SpatialPoints'
adjacency_matrix(x, ...)

## S3 method for class 'sf'
adjacency_matrix(x, ...)

## Default S3 method:
adjacency_matrix(x, ...)

Arguments

x

Raster, SpatialPolygons, SpatialLines, or sf::sf() object representing planning units.

...

not used.

directions

integer If x is a Raster object, the number of directions in which cells should be considered adjacent: 4 (rook's case), 8 (queen's case), 16 (knight and one-cell queen moves), or "bishop" to for cells with one-cell diagonal moves.

Details

Spatial processing is completed using sf::st_intersects() for Spatial and sf::sf() objects, and raster::adjacent() for Raster objects.

Value

dsCMatrix sparse symmetric matrix. Each row and column represents a planning unit. Cells values indicate if different planning units are adjacent to each other or not (using ones and zeros). To reduce computational burden, cells among the matrix diagonal are set to zero. Furthermore, if the argument to x is a Raster object, then cells with NA values are set to zero too.

Notes

In earlier versions (< 5.0.0), this function was named as the connected_matrix function. It has been renamed to be consistent with other spatial association matrix functions.

Examples

# load data
data(sim_pu_raster, sim_pu_sf, sim_pu_lines)

# create adjacency matrix using raster data
## crop raster to 9 cells
r <- crop(sim_pu_raster, c(0, 0.3, 0, 0.3))

## make adjacency matrix
am_raster <- adjacency_matrix(r)

# create adjacency matrix using polygons (sf) data
## subset 9 polygons
ply <- sim_pu_sf[c(1:2, 10:12, 20:22), ]

## make adjacency matrix
am_ply <- adjacency_matrix(ply)

# create adjacency matrix using lines (Spatial) data
## subset 9 lines
lns <- sim_pu_lines[c(1:2, 10:12, 20:22), ]

## make adjacency matrix
am_lns <- adjacency_matrix(lns)

# plot data and the adjacency matrices
## Not run: 
par(mfrow = c(4,2))

## plot raster and adjacency matrix
plot(r, main = "raster", axes = FALSE, box = FALSE)
plot(raster(as.matrix(am_raster)), main = "adjacency matrix", axes = FALSE,
     box = FALSE)

## plot polygons (sf) and adjacency matrix
plot(r, main = "polygons (sf)", axes = FALSE, box = FALSE)
plot(raster(as.matrix(am_ply)), main = "adjacency matrix", axes = FALSE,
    box = FALSE)

## plot lines (Spatial) and adjacency matrix
plot(r, main = "lines (Spatial)", axes = FALSE, box = FALSE)
plot(raster(as.matrix(am_lns)), main = "adjacency matrix", axes = FALSE,
     box = FALSE)

## End(Not run)

prioritizr

Systematic Conservation Prioritization in R

v7.0.1
GPL-3
Authors
Jeffrey O Hanson [aut] (<https://orcid.org/0000-0002-4716-6134>), Richard Schuster [aut, cre] (<https://orcid.org/0000-0003-3191-7869>), Nina Morrell [aut], Matthew Strimas-Mackey [aut] (<https://orcid.org/0000-0001-8929-7776>), Matthew E Watts [aut], Peter Arcese [aut] (<https://orcid.org/0000-0002-8097-482X>), Joseph Bennett [aut] (<https://orcid.org/0000-0002-3901-9513>), Hugh P Possingham [aut] (<https://orcid.org/0000-0001-7755-996X>)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.