R how to leave only outer border of census tract map? geom_sf, tigris

384 views Asked by At

I'm creating a choropleth map and trying to draw borderline for metropolitan statistical areas. I managed to get it done mostly, but I want to leave only the outer borderline and get rid of the lines for smaller tracts within the area. Below is a reproducible lines of codes. Thanks in advance for all the help and time!

library(tigris)
library(sf)
library(tidyverse)
options(tigris_class = "sf")
options(tigris_use_cache = TRUE)

## Getting the Metro & Micro areas
ks <- tracts("KS", cb = T)
cb <- core_based_statistical_areas(cb = T)
str(cb)
# Filter for Kansas metro
kmsa <- cb %>% 
  filter(grepl("KS", NAME)) %>% 
  filter(LSAD == "M1")

ggplot(kmsa) + geom_sf()

# Leave only within KS
w1 <- st_within(ks, kmsa)

w2 <- map_lgl(w1, function(x) {
  if (length(x) == 1) {
    return(TRUE)
  } else {
    return(FALSE)
  }
})

# Subset 
kmsa2 <- ks[w2,]

# Plot
ggplot() + 
  geom_sf(data = ks, fill = "grey50", lwd = 0) +
  geom_sf(data = kmsa2, fill = NA, color = "white") +
  theme_minimal() +
  theme(axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.x = element_blank(),
        axis.ticks.y = element_blank(),
        panel.grid.major = element_blank())
0

There are 0 answers