如何为地图中的每个人口普查区添加更多数据?

How do I add more data for each census tracts in the map?

我能够使用 tidycensustigris[ 准备一个县的人口普查地图(显示所有人口普查地图) =33=]。我在一个名为 demography 的单独数据框中有一些数据,其中包含 4 列 countytractx.foreclosure_filingdelinquent_parcels

如何创建仅包含 demography(仅 19 个区域)数据框中的那些区域的地图,并显示 x.foreclosure_filing 的值,以及 delinquent_parcels 的值( 19) 地图中的大片?

人口统计数据框如下所示:

County      tract           X.foreclosure_filings   delinquent_parcels
1 Cuyahoga 1401.00                     8              13.52
2 Cuyahoga 1403.01                    18              22.25
3 Cuyahoga 1403.02                    18              11.96
4 Cuyahoga 1404.00                    19               8.44
5 Cuyahoga 1405.00                    27              10.93
6 Cuyahoga 1407.01                    17              13.77

代码

library(tidycensus)
library(tidyverse)
options(tigris_use_cache = TRUE)


clevelandhts <- get_acs(state = "OH", county = "Cuyahoga", geography = "tract", 
                        variables = "B19013_001", geometry = TRUE)

View(clevelandhts)
clevelandhts %>%
  ggplot(aes(fill = estimate)) + 
  geom_sf(color = NA) + 
  coord_sf(crs = 26917) + 
  scale_fill_viridis_c(option = "magma")

您可以使用 fuzzy_join 将您的两个数据框合并在一起,使用 str_detectdemography 中找到包含在 NAME 中的人口普查字符串 [=17] =].要将地图中不同列的信息作为标签,请使用 geom_sf_label.

编辑:填充颜色现在基于X.foreclosure_filings

library(tidycensus)
library(tidyverse)
library(fuzzyjoin)

options(tigris_use_cache = TRUE)

demography$tract <- as.character(demography$tract)

census_api_key("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")

clevelandhts <- get_acs(state = "OH", county = "Cuyahoga", geography = "tract", 
                        variables = "B19013_001", geometry = TRUE)

clevelandhts %>%
  fuzzy_join(demography, by = c("NAME" = "tract"), match_fun = str_detect) %>%
  ggplot(aes(fill = X.foreclosure_filings)) + 
  geom_sf(color = NA) + 
  coord_sf(crs = 26917) + 
  scale_fill_viridis_c(option = "magma") +
  geom_sf_label(aes(label = X.foreclosure_filings))

情节

数据

demography <- read.table(
  text = "County      tract           X.foreclosure_filings   delinquent_parcels
 Cuyahoga 1401.00                     8              13.52
 Cuyahoga 1403.01                    18              22.25
 Cuyahoga 1403.02                    18              11.96
 Cuyahoga 1404.00                    19               8.44
 Cuyahoga 1405.00                    27              10.93
 Cuyahoga 1407.01                    17              13.77", header = T)

编辑 (2/29/20):

要在下方添加街道地图,您可以执行以下操作。

在此示例中,我在输入 API 键后使用 Google 街道地图。 location 参数特定于此示例,但可以在加入 demography table 后从 clevelandhts 获得框边界。同样,这只是一个演示。如果您需要进一步的帮助,我鼓励您 post 提出一个单独的问题。

# Requires Google API key
county_map <- get_map(location = c(-81.57,41.49,-81.52,41.56), maptype = "roadmap", source = "google")

full_data <- fuzzy_join(clevelandhts, demography, by = c("NAME" = "tract"), match_fun = str_detect)

ggmap(county_map) +
  geom_sf(data = full_data, inherit.aes = FALSE, aes(fill = X.foreclosure_filings)) + 
  scale_fill_viridis_c(option = "magma", alpha = .2) +
  geom_sf_label(data = full_data, aes(label = X.foreclosure_filings), inherit.aes = FALSE)