I'm trying to create a county-level map showing counties where certain events took place, and counties adjacent to them. I have determined adjacency using sf and extracted the adjacent county names into a list of vectors, but I am not quite sure how to impute a variable to my data frame that includes adjacent counties. In plain English, I need a case_when statement that checks each row of nc for a value of anything but 1 in the nc$aliens column, and returns a 1 when the value of nc$NAME on a given row is contained in the vector of neighboring county names of a county whose value is set to 1. For example, my reprex sets Buncombe County to 1, so I would also like to return a 1 for Yancey County because it is adjacent.
library(sf)
#> Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1; sf_use_s2() is TRUE
library(dplyr)
#> Warning: package 'dplyr' was built under R version 4.1.3
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(ggplot)
#> Error in library(ggplot): there is no package called 'ggplot'
##LOAD NC SHAPEFILE
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
##CALCULATE NEIGHBORING POLYGONS
neighbors <- st_intersects(nc, nc)
##EXTRACT NAMES OF NEIGHBORING POLYGONS/COUNTIES
neighbor_names <- lapply(neighbors, function(x) {nc$NAME[x]})
names(neighbor_names) <- nc$NAME
##ADD COLUMN INDICATING COUNTIES CONTAINING ALIEN CRASH SITES
nc$aliens <- case_when(
nc$NAME == 'Martin' ~ 1,
nc$NAME == 'Buncombe' ~ 1,
TRUE ~ 0
)
ggplot()+
geom_sf(data = nc, aes(fill= factor(aliens)))
#> Error in ggplot(): could not find function "ggplot"
Created on 2023-07-14 with reprex v2.0.2