简体   繁体   English

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

[英]Plotting both state AND county boundaries on same map using plot_usmap from usmap package in R

I would like to create a map of the US showing both state and county boundaries (ie state boundaries in a different color).我想创建一张美国地图,显示州和县边界(即不同颜色的州边界)。 I typically do this using either shape files that I import or using ggplot2 's map_data function.我通常使用导入的形状文件或使用ggplot2map_data函数来执行此操作。 However, I face three obstacles.但是,我面临三个障碍。

1) I cannot install gdal and geos in my computing environment so that precludes the use of any shape files or GeoJSON files (my attempts to map county level shape files loaded using fastshp have not been successful but I'm open to any solution that can reproduce the map below but with state boundaries included). 1) 我无法在我的计算环境中安装gdalgeos ,因此无法使用任何形状文件或 GeoJSON 文件(我尝试映射使用fastshp加载的县级形状文件没有成功,但我愿意接受任何可以的解决方案复制下面的地图,但包括州界)。

2) I need to include Hawaii and Alaska, so that excludes the use of map_data from ggplot2 . 2)我需要包括夏威夷和阿拉斯加,这样就排除了map_dataggplot2的使用。

3) I need the map to include both state AND county boundaries, which makes the use of usmap package problematic as its a wrapper function for ggplot2 but without the ease and general ability to customize to the level of a raw ggplot2 object. 3)我需要地图包含州和县边界,这使得使用usmap包作为usmap的包装函数存在ggplot2但没有自定义到原始 ggplot2 对象级别的简单和通用能力。

4) Also, cannot make use of sf package bc it has a non R library dependency ( units package depends on C library libudunits2 ). 4) 此外,不能使用sf包 bc 它具有非 R 库依赖项( units包依赖于 C 库libudunits2 )。

What I need: A map that can project Alaska and Hawaii and display state and county boundaries using contrasting colors and I need to accomplish all this without resorting to any packages that rely on rgeos , rgdal , and/or units .我需要什么:可以投​​影阿拉斯加和夏威夷并使用对比色显示州和县边界的地图,我需要完成所有这一切,而无需求助于任何依赖rgeosrgdal和/或units

What I've tried thus far plot_usmap from the usmap package:到目前为止plot_usmapusmap包中尝试过的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

What I suspect is happening is that one layer (the original ggplot code) is projected using a different CRS system than the other layer -generated by plot_usmap .我怀疑正在发生的是,一层(原始ggplot代码)是使用与由plot_usmap生成的plot_usmap不同的 CRS 系统plot_usmap That second layer results in a very small red dot (see circle in map below).第二层会产生一个非常小的红点(见下图中的圆圈)。 Not sure how to re-project without geos/gdal installed.不知道如何在没有安装 geos/gdal 的情况下重新投影。 See the map below with the black circle highlighting where the red dot is.请参阅下面的地图,黑色圆圈突出显示红点所在的位置。

在此处输入图片说明

Ok after some suggestions from the package author and some of my own tinkering around I was finally able to get my desired output.好的,在包作者​​的一些建议和我自己的一些修补之后,我终于能够得到我想要的输出。

This approach is ideal for folks looking to generate a US map w/ Alaska and Hawaii included who...这种方法非常适合希望生成包含阿拉斯加和夏威夷在内的美国地图的人...

1) Do not have the ability to install non-R packages in the environment their R engine is running on (eg lack admin access) 1) 无法在其 R 引擎运行的环境中安装非 R 包(例如,缺乏管理员访问权限)

2) Need to map both county and state boundaries using contrasting colors 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)

Using the layers meta info already embedded in the data from us_map使用已经嵌入到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

Using just the raw data obtained from the us_map package仅使用从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')

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM