首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用R中usmap包中的plot_usmap绘制同一地图上的州和县边界

使用R中usmap包中的plot_usmap绘制同一地图上的州和县边界
EN

Stack Overflow用户
提问于 2020-01-22 02:30:32
回答 1查看 2.2K关注 0票数 5

我想要创建一个美国地图,显示州和县的边界(即不同颜色的州边界)。我通常使用导入的形状文件或使用ggplot2map_data函数来实现这一点,但是,我面临三个障碍。

1)我无法在我的计算环境中安装gdalgeos,以避免使用任何形状文件或GeoJSON文件(我使用fastshp加载的县级形状文件的映射尝试没有成功,但我愿意使用任何解决方案来复制下面的地图,但包含州边界)。

2)我需要包括夏威夷和阿拉斯加,这样ggplot2就不允许使用ggplot2了。

3)我需要地图同时包含州和县边界,这使得usmap包的使用成为ggplot2的包装函数,但不具备定制到原始ggplot2对象级别的方便和通用能力。

4)另外,不能使用sf包bc,它具有非R库依赖关系(units包依赖于C库libudunits2)。

我需要的是:一张地图,可以投射阿拉斯加和夏威夷,并使用对比颜色显示州和县的边界,我需要完成所有这一切,而不需要使用任何依赖rgeosrgdal和/或units的包。

到目前为止,我尝试过从plot_usmap包中提取usmap

代码语言:javascript
复制
library(dplyr)
library(stringr)
library(ggplot2)
library(usmap)
library(mapproj)
devtools::install_github("wmurphyrd/fiftystater")
library(fiftystater)

county_data<-read.csv("https://www.ers.usda.gov/webdocs/DataFiles/48747/PovertyEstimates.csv?v=2529") %>% #
  filter(Area_name != "United States") %>%
  select(FIPStxt, Stabr, Area_name, PCTPOVALL_2017) %>%
  rename(fips = FIPStxt)
crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)
state_map <- map_data("state")

plot_usmap(data = county_data, values = "PCTPOVALL_2017", color = "white") + 
  geom_map(data = crimes, aes(map_id = state), map = fifty_states, color= "red") + 
  geom_path(data = state_map, aes(x =long , y=lat), color= "red")+
  expand_limits(x = fifty_states$long, y = fifty_states$lat) +
  theme(legend.position = "none") +
  theme_map() #no go

plot_usmap(data = county_data, values = "PCTPOVALL_2017", color = "white") + 
  geom_map(data = crimes, aes(map_id = state), map = fifty_states, color= "red") + 
  expand_limits(x = fifty_states$long, y = fifty_states$lat) +
  theme(legend.position = "none") +
  theme_map() #no go

plot_usmap(data = county_data, values = "PCTPOVALL_2017", color = "white") + 
  geom_map(data = crimes, aes(map_id = state, color= "red"), map = fifty_states) + 
  expand_limits(x = fifty_states$long, y = fifty_states$lat) +
  theme(legend.position = "none") +
  theme_map() #no go

我怀疑发生的情况是,一个层(原始的ggplot代码)是使用与另一层-generated不同的plot_usmap系统投射的。第二层的结果是一个很小的红色点(见下图中的圆圈)。不确定如何在没有安装geos/gdal的情况下重新设计。请参阅下面的地图,黑圈突出显示红点所在的位置。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-05 22:10:58

好吧,经过一些来自包作者的建议和我自己的一些修补,我终于能够得到我想要的输出。

这种方法是理想的,人们希望生成美国地图w/阿拉斯加和夏威夷,包括谁.

1)无法在其R引擎运行的环境中安装非R包(例如缺少管理权限)

( 2)需要用对比颜色来绘制县和州的边界。

代码语言:javascript
复制
library(dplyr)
library(ggplot2)
library(usmap)

#Example data (poverty rates)
county_data<-read.csv("https://www.ers.usda.gov/webdocs/DataFiles/48747/PovertyEstimates.csv?v=2529") %>% #
  filter(Area_name != "United States") %>%
  select(FIPStxt, Stabr, Area_name, PCTPOVALL_2018) %>%
  rename(fips = FIPStxt)

states <- plot_usmap("states", 
                     color = "red",
                     fill = alpha(0.01)) #this parameter is necessary to get counties to show on top of states
counties <- plot_usmap(data = county_data, 
                       values = "PCTPOVALL_2018",
                       color = "black",
                       size = 0.1)

使用已嵌入us_map数据中的图层元信息

代码语言:javascript
复制
ggplot() +
  counties$layers[[1]] + #counties needs to be on top of states for this to work
  states$layers[[1]] +
  counties$theme + 
  coord_equal() +
  theme(legend.position="none") +
  scale_fill_gradient(low='white', high='grey20') #toggle fill schema using vanilla ggplot scale_fill function

仅使用从us_map包获得的原始数据

代码语言:javascript
复制
ggplot() +  
  geom_polygon(data=counties[[1]], 
               aes(x=x, 
                   y=y, 
                   group=group, 
                   fill = counties[[1]]$PCTPOVALL_2018), 
               color = "black",
               size = 0.1) +  
  geom_polygon(data=states[[1]], 
               aes(x=x, 
                   y=y, 
                   group=group), 
               color = "red", 
               fill = alpha(0.01)) + 
  coord_equal() +
  theme_map() +
  theme(legend.position="none") +
  scale_fill_gradient(low='white', high='grey20')

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

https://stackoverflow.com/questions/59851823

复制
相关文章

相似问题

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