【发布时间】:2020-05-08 03:31:45
【问题描述】:
我想创建一张美国地图,同时显示州和县的边界(即不同颜色的州边界)。我通常使用我导入的形状文件或使用ggplot2 的map_data 函数来执行此操作。但是,我面临三个障碍。
1) 我无法在我的计算环境中安装 gdal 和 geos,因此无法使用任何形状文件或 GeoJSON 文件(我尝试映射使用 fastshp 加载的县级形状文件没有成功,但是我愿意接受任何可以重现以下地图但包含州界的解决方案。
2) 我需要包括夏威夷和阿拉斯加,这样就排除了 map_data 从 ggplot2 的使用。
3) 我需要地图同时包含州和县边界,这使得使用 usmap 包作为 ggplot2 的包装函数存在问题,但没有轻松和通用的自定义能力原始 ggplot2 对象。
4) 此外,不能使用 sf 包 bc 它具有非 R 库依赖项(units 包依赖于 C 库 libudunits2)。
我需要的是:一张可以投影阿拉斯加和夏威夷并使用对比色显示州和县边界的地图,我需要完成所有这一切,而不需要依赖于 rgeos、rgdal 和/或的任何软件包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 中导入任何县级多边形数据,我想您需要联系包作者。 -
据我所知,这个包是几年前写的,作者已经离开了。但值得发送消息并寻求帮助。