使用R中usmap包中的plot_usmap在同一地图上绘制州和县边界 [英] Plotting both state AND county boundaries on same map using plot_usmap from usmap package in R

查看:402
本文介绍了使用R中usmap包中的plot_usmap在同一地图上绘制州和县边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to create a map of the US showing both state and county boundaries (i.e. state boundaries in a different color). I typically do this using either shape files that I import or using ggplot2's map_data function. However, I face three obstacles.

1)我无法在计算环境中安装gdalgeos,从而无法使用任何形状文件或GeoJSON文件(我尝试映射使用fastshp加载的县级形状文件的尝试并未成功,但我愿意接受任何可以复制下面的地图但包含状态边界的解决方案.

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

2)我需要包括夏威夷和阿拉斯加,以便从ggplot2中排除使用map_data.

2) I need to include Hawaii and Alaska, so that excludes the use of map_data from ggplot2.

3)我需要地图同时包含州和县的边界,这使得使用usmap包成为ggplot2的包装函数时遇到了问题,但没有轻松和通用的能力来定制a原始ggplot2对象.

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.

4)另外,它不能使用sf包,因为它具有非R库依赖项(units包取决于C库libudunits2).

4) Also, cannot make use of sf package bc it has a non R library dependency (units package depends on C library libudunits2).

我需要什么:一张可以投射阿拉斯加和夏威夷并使用对比色显示州和县边界的地图,而我需要完成所有这些工作,而无需依靠任何依赖rgeosrgdal和/或units.

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.

到目前为止,我从usmap软件包尝试的内容plot_usmap:

What I've tried thus far plot_usmap from the usmap package:

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

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. 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. 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)无法在非R软件包中安装非R软件包. 运行R引擎的环境(例如缺少管理员访问权限)

1) Do not have the ability to install non-R packages in the environment their R engine is running on (e.g. lack admin access)

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

2) Need to map both county and state boundaries using contrasting colors

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

数据中的图层元信息

Using the layers meta info already embedded in the data from 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包中获取的原始数据

Using just the raw data obtained from the us_map package

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

这篇关于使用R中usmap包中的plot_usmap在同一地图上绘制州和县边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆