首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >基于R中城市地图的县地块覆盖州轮廓

基于R中城市地图的县地块覆盖州轮廓
EN

Stack Overflow用户
提问于 2020-03-25 01:32:37
回答 1查看 401关注 0票数 0

我试图用ubrbanmapr将美国各州的大纲添加到县数据的合唱中。我既可以绘制州大纲,也可以画县合唱,但不能用州大纲来画县合唱。如有任何指导,将不胜感激。

守则:

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-25 02:17:21

不要使用geom_polygon,添加另一个geom_sf。

代码语言:javascript
复制
#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())

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60841840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档