【问题标题】:Plotting both state AND county boundaries on same map using plot_usmap from usmap package in R使用 R 中 usmap 包中的 plot_usmap 在同一张地图上绘制州和县边界
【发布时间】:2020-05-08 03:31:45
【问题描述】:

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

1) 我无法在我的计算环境中安装 gdalgeos,因此无法使用任何形状文件或 GeoJSON 文件(我尝试映射使用 fastshp 加载的县级形状文件没有成功,但是我愿意接受任何可以重现以下地图但包含州界的解决方案。

2) 我需要包括夏威夷和阿拉斯加,这样就排除了 map_dataggplot2 的使用。

3) 我需要地图同时包含州和县边界,这使得使用 usmap 包作为 ggplot2 的包装函数存在问题,但没有轻松和通用的自定义能力原始 ggplot2 对象。

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

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

到目前为止我从usmap 包中尝试过的plot_usmap

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 代码)使用与另一层不同的 CRS 系统进行投影 - 由 plot_usmap 生成。第二层会产生一个非常小的红点(见下图中的圆圈)。不确定如何在没有安装 geos/gdal 的情况下重新投影。请参阅下面的地图,黑色圆圈突出显示红点所在的位置。

【问题讨论】:

  • 有机会使用你上次使用的fastshp 包吗?
  • 好的。让我试试。
  • 我仍然无法使用 read.shp() 读取任何 shapefile。我不知道为什么。 this package by hrbrmstr 有你可能感兴趣的 shapefile。我为你创建了 shapefile。如果可以,可以尝试从my git 下载它们吗?如果您可以使用 read.shp() 读取这些文件,我认为您将有很好的机会绘制您想要的地图。
  • 因为我看不到你这边发生了什么,所以我无能为力。但是如果你能成功导入县级shapefile,你想看看你有什么样的信息。你能找到parts吗?那是指示每个多边形的索引的列。据推测,每个县都应该有一个索引号。如果您可以识别此类信息,您需要为每个州的每个县创建组变量,就像我上次为您所做的那样。如果您无法在 R 中导入任何县级多边形数据,我想您需要联系包作者。
  • 据我所知,这个包是几年前写的,作者已经离开了。但值得发送消息并寻求帮助。

标签: r ggplot2 mapping ggmap


【解决方案1】:

好的,在包作者​​的一些建议和我自己的一些修补之后,我终于能够得到我想要的输出。

这种方法非常适合希望生成包含阿拉斯加和夏威夷在内的美国地图的人们......

1) 没有能力安装非 R 包 运行 R 引擎的环境(例如,缺乏管理员访问权限)

2) 需要使用对比绘制县和州边界 颜色

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 的数据中已经嵌入的层元信息

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 包中获得的原始数据

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')

【讨论】:

  • 使用层元信息方法,我得到Error in FUN(X[[i]], ...) : object 'x' not found。事实上,ggplot() + usmap$layers 的任何变化都会引发相同的错误。知道发生了什么吗?
  • 我必须查看您的代码才能进行故障排除。如果您按照上面的确切步骤进行操作,您应该能够复制。
  • 我复制粘贴了您的代码以确保,但它给了我同样的错误。重现错误的最简单代码:ggplot() + plot_usmap()$layers[[1]]。我正在使用 ggplot2_3.3.2 和 usmap_0.5.1,以防万一。
  • Ofc,它不是。但是您的代码也不起作用。我只是展示了重现错误的最简单方法。为了清楚起见,您的第二个版本(仅使用从 us_map 包获得的原始数据)有效。第一个没有。
  • 对于其他有同样问题的人来说,usmap 0.5.0 似乎破坏了这个功能。正在修复错误。同时,使用较早的版本来让它工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-01
  • 2018-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多