sfweight icon indicating copy to clipboard operation
sfweight copied to clipboard

create wrapper for nb2lines this is for plotting / network work

Open JosiahParry opened this issue 3 years ago • 1 comments

JosiahParry avatar Jun 21 '21 16:06 JosiahParry


acs <- select(uitk::acs_raw, 
              fips = ct_id_10, med_house_income, 
              by_pub_trans, bach) %>% 
  mutate(fips = as.character(fips),
         across(.cols = c(med_house_income, by_pub_trans, bach), 
                ~replace_na(.x, median(.x, na.rm = TRUE))))


acs_sf <- left_join(uitk::suffolk_county, acs, by = "fips")

acs_nb <- poly2nb(acs_sf)

neighbors_tidy <- nb2lines(acs_nb, coords = st_geometry(acs_sf), as_sf = TRUE)

ggplot() +
  geom_sf(data = acs_sf, alpha = 0.2, color = "grey60", fill = NA) +
  geom_sf(data = neighbors_tidy) +
  geom_sf(data = st_centroid(acs_sf)) +
  theme_void() + 
  labs(title = "Neighbor relationships for Suffolk County")

JosiahParry avatar Jun 21 '21 16:06 JosiahParry