Skip to contents

This function takes in a polygon object and its corresponding neighborhood object, and plots the polygons and their neighbors on a map.

Usage

plot_neighbors(poly_obj, nb_obj)

Arguments

poly_obj

A SpatialPolygonsDataFrame representing the polygons.

nb_obj

A nb object generated using the 'spdep' package representing the neighborhood relationship information.

Value

A ggplot object with the plotted polygons and their neighboring relationships.

Details

The function first extracts the coordinates from poly_obj and creates a data frame named coords with columns "long" and "lat". Then, it generates a set of lines representing the neighboring relationships between polygons using the nb2lines function. It then plots the polygons and their neighbors on a map using ggplot2.

Examples

library(sp)
library(spdep)
library(ggplot2)
library(sf)

data(columbus)
nb_obj <- dnearneigh(coordinates(columbus), 0, 10000)
#> Error in dnearneigh(coordinates(columbus), 0, 10000): ncol(x) == 2L || ncol(x) == 3L is not TRUE
poly_obj <- columbus

plot_neighbors(poly_obj, nb_obj)
#> Error in nb2lines(nb_obj, coords = coords, proj4string = proj4string(poly_obj)): object 'nb_obj' not found