TLDR: Trying to graph some simple US data by census regions and I wanted to put an outline surrounding the regions, but I can't seem to get the regional shapefiles to work.
Hi All,
I'm new to graphing geographic data in R and am trying something simple (US data by census region) but can't quite get some of my design elements to work. I was trying the code from this post (Mapping by US Census Divisions in ggplot2) and can't seem to get the shapefile for the outlines of the census regions to work properly. Here's the code from the other post:
library(tidyverse)
library(tigris)
library(sf)
library(maps)
div_dat <- states(cb = FALSE, resolution = '20m') %>%
st_drop_geometry() %>%
select(NAME, DIVISION) %>%
mutate(ID = tolower(NAME))
# get state data, convert to sf, join with division data
states <- maps::map("state", plot = FALSE, fill = TRUE) %>%
st_as_sf() %>%
left_join(div_dat)
# create division polygons
div <- states %>%
group_by(DIVISION) %>%
summarize()
# plot it
ggplot() +
theme_void() +
geom_sf(data = states,
aes(fill = as.numeric(DIVISION)),
color = 'white') +
geom_sf(data = div,
color = 'black',
fill = NA,
size = 1) +
scale_fill_viridis_c() +
coord_sf(crs = 5070) +
labs(fill = NULL)
However, whenever I get to the block to create division polygons I get this error:
Error in wk_handle.wk_wkb(wkb, s2_geography_writer(oriented = oriented, : Loop 0 is not valid: Edge 236 crosses edge 239
I imagine this is something simple that I'm doing wrong with the shapefiles, but I'm not sure what the issue is, or how to solve it. I tried turning off spherical geometry and using st_make_valid() but neither of them solved the issue. How should I try to fix this?

Two ways around this, and I'm not sure which is better. #1 is to change the crs (I used web mercator '3857'), make it valid, and then change the crs back to the original. #2 is to set
sf_use_s2(FALSE).Both examples below:
Created on 2023-04-19 by the reprex package (v2.0.1)