[英]Overlay State Outlines on County Plot using urbanmapr in R
我正在嘗試使用 ubbanmapr 將美國各州的輪廓添加到縣數據的總體中。 我可以繪制州輪廓或縣區划,但不能繪制帶有州輪廓的縣區划。 任何指導表示贊賞。
編碼:
library(urbnmapr)
library(ggplot2)
library(dplyr)
# Obtain county polygon data
counties_sf <- get_urbn_map(map = "counties", sf = TRUE)
# Assign random values of data to each count
counties_sf$value = runif(length(counties_sf$county_fips), min=-1.0, max=1.0)
# Remove AK and HI - lower 48 only
counties_sf <- counties_sf[!(counties_sf$state_abbv %in% c("HI","AK")),]
# Plot county level data
counties_sf %>%
ggplot() +
# Overlay State Outlines
geom_polygon(data = urbnmapr::states, mapping = aes(long, lat, group = group),
fill = NA, color = "black", size = 0.25) +
# Plot county data and fill with value
geom_sf(mapping = aes(fill = value), color = NA) +
# Remove grid lines from plot
coord_sf(datum = NA) +
labs(fill = "Random Data") +
scale_fill_gradient2(low='blue', high='red') +
theme_bw() +
theme(
# Hide panel borders and remove grid lines
panel.border = element_blank())
謝謝你的幫助
jfd118
不要使用geom_polygon,添加另一個geom_sf。
#devtools::install_github("UrbanInstitute/urbnmapr")
library(urbnmapr)
library(ggplot2)
library(dplyr)
# Obtain county polygon data
states_sf <- get_urbn_map(map = "states", sf = TRUE)
counties_sf <- get_urbn_map(map = "counties", sf = TRUE)
# Assign random values of data to each count
set.seed(1234)
counties_sf$value = runif(length(counties_sf$county_fips), min=-1.0, max=1.0)
# Plot county level data
ggplot() +
# Plot county data and fill with value
geom_sf(data=counties_sf, mapping = aes(fill = value), color = NA) +
# Overlay State Outlines
geom_sf(data = states_sf, fill = NA, color = "black", size = 0.25) +
# Remove grid lines from plot
coord_sf(datum = NA) +
labs(fill = "Random Data") +
scale_fill_gradient2(low='blue', high='red') +
theme_bw() +
theme(
# Hide panel borders and remove grid lines
panel.border = element_blank())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.