0

我能够使用 和 准备一个县的人口普查区地图(显示所有人口普查区tidycensustigris。我在一个名为的单独数据框中有一些数据,其中demography包含 4 列county、、、和。tractx.foreclosure_filingdelinquent_parcels

如何仅创建demography(仅 19 个)数据框中的那些区域的地图,并在地图中显示x.foreclosure_filingdelinquent_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")
4

1 回答 1

0

您可以使用fuzzy_join将两个数据框合并在一起,并str_detect从中找到demography包含NAMEclevelandhts. 要将地图中不同列的信息作为标签包含在内,请使用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 参数是本例特有的,但可以clevelandhts在与demography表连接后获得框边界。同样,这只是一个演示。如果您需要进一步的帮助,我鼓励您发布一个单独的问题。

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

带有街景的地图

于 2020-02-28T17:00:44.623 回答