我正在创建一个等值线地图并尝试为大都市统计区域绘制边界。我设法完成了大部分工作,但我只想留下外部边界并摆脱该区域内较小区域的线条。下面是可重现的代码行。提前感谢所有的帮助和时间!
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())