在 USMAP 中绘制旧康涅狄格州县区域

问题描述 投票:0回答:1

最近,美国人口普查局更改了康涅狄格州报告的地理实体,如此处所述。此前,以下 8 个县报告了数据:

CT_counties <- data.frame(county = c("Fairfield County, CT",
                                 "Hartford County, CT",
                                 "Litchfield County, CT",
                                 "Middlesex County, CT",
                                 "New Haven County, CT",
                                 "New London County, CT",
                                 "Tolland County, CT",
                                 "Windham County, CT"),
                      fips = c("09001",
                               "09003",
                               "09005",
                               "09007",
                               "09009",
                               "09011",
                               "09013",
                               "09015"))

由于

usmap
已更新以促进这一变化,因此使用的新9个规划区域是:

CT_regions <- data.frame(region = c("Capitol Planning Region",
                             "Greater Bridgeport Planning Region",
                             "Lower Connecticut River Valley Planning Region",
                             "Naugatuck Valley Planning Region",
                             "Northeastern Connecticut Planning Region",
                             "Northwest Hills Planning Region",
                             "South Central Connecticut Planning Region",
                             "Southeastern Connecticut Planning Region",
                             "Western Connecticut Planning Region"),
                  fips = c("09110",
                           "09120",
                           "09130",
                           "09140",
                           "09150",
                           "09160",
                           "09170",
                           "09180",
                           "09190"))

因为

usmap
使用新的 shapefile,所以以下代码有效:

library(usmap)
plotdf <- data.frame(fips = CT_regions$fips, vals = rnorm(9))
plot_usmap(data = plotdf, values = "vals",
       include = c("MA", "CT", "RI"),
       labels = FALSE)

生成以下图像: enter image description here

但是,由于大量数据是在较旧的县一级报告的,因此仍然能够绘制这些数据是有用的。然而,这不再有效。以下代码没有提供任何有用的信息:

library(usmap)
plotdf <- data.frame(fips = CT_counties$fips, vals = rnorm(8))
plot_usmap(data = plotdf, values = "vals",
       include = c("MA", "CT", "RI"),
       labels = FALSE)

问题: 使用

usmap
,有没有简单的方法来绘制以前用于康涅狄格州 (
CT_counties
) 的县?特别是一种不涉及回到以前版本的
usmap
的方式。因为能够在同一个脚本中完成这两件事非常有用。

r ggplot2 usmap
1个回答
0
投票

为了详细阐述我的评论,创建 2021 年以及康涅狄格州转向规划区域之前几年的分区统计图的工作流程将如下所示:

首先,我们使用

tigris::counties
函数获取 TIGER shapeline 文件。请注意,由于我们直接访问 TIGER 文件,因此我们不会在工作流程中使用
usmap("counties")
,因为它使用 2022 年的 FIPS 代码。

library(tigris)
library(dplyr)
library(ggplot2)

options(tigris_use_cache = TRUE)

CT_county_shp <- counties(state = "CT", year = 2021)

CT_counties <- data.frame(
  county = c(
    "Fairfield County, CT",
    "Hartford County, CT",
    "Litchfield County, CT",
    "Middlesex County, CT",
    "New Haven County, CT",
    "New London County, CT",
    "Tolland County, CT",
    "Windham County, CT"
  ),
  fips = c(
    "09001",
    "09003",
    "09005",
    "09007",
    "09009",
    "09011",
    "09013",
    "09015"
  ),
# This can be any variable(s) as long as we have the correct FIPS codes
  vals = rnorm(8)
)

接下来,我们将县数据框加入到从美国人口普查局下载的县形状文件中(我在这里使用

tidyverse
工具,但基本 R 或
data.table
也可以工作):

CT_counties_clean <- left_join(
  CT_county_shp,
  CT_counties,
  by = join_by("GEOID"=="fips"),
  keep = TRUE
) |>
  select(
    county,
    fips,
    vals,
    geometry
  )

我们的

CT_counties_clean
数据框也应该是一个简单的特征,或
sf
,对象(例如,
class(CT_counties_clean)
同时返回
data.frame
sf
)。
geometry
列是包含多多边形几何形状的
list
列,与
geom
usmap("counties")
列相同。由于
plot_usmap()
在底层使用
ggplot()
,但不允许我们将数据连接到 2022 年县以外的任何数据,因此我们可以构建自己的
ggplot()
,例如:

ggplot() +
  geom_sf(data = CT_counties_clean, aes(fill = vals)) +
  labs(title = "CT Counties (2021)") +
  theme_void()

Choropleth of arbitrary values for Connecticut counties 2021

从那里,您可以根据自己的喜好修改地图,添加标签、自定义颜色、字体等,以及使用地图投影。

usmap_crs()
表示
usmap
对象的默认 CRS 是 EPSG 9311 坐标参考系,而从
tigris
获取 CT 县并运行
sf::st_crs()
显示 TIGER 文件位于 EPSG 4269 坐标参考系中。您需要确保 2021 年和 2022 年康涅狄格州县地图使用相同的 CRS。希望这对您有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.