Chr*_*her 5 plot r census choroplethr
我的问题涉及通过choroplethr和choroplethrZip在MSA级别上绘制整个美国地图.
在下面的示例中,我们绘制1)美国县级地图上的人口普查人口信息和2)选定的城市/微观统计区域(MSA)级别的缩放地图.
示例R代码:
library(choroplethr)
library(choroplethrZip)
?zip.regions
data(zip.regions)
head(zip.regions)
?df_pop_county
data(df_pop_county)
df_pop_county
?df_pop_zip
data(df_pop_zip)
# U.S. County Population Data
county_choropleth(df_pop_county, legend = "Population")
# NY-NJ-PA MSA Population Data
zip_choropleth(df_pop_zip,
msa_zoom = "New York-Newark-Jersey City, NY-NJ-PA",
title = "2012 NY-Newark-Jersey City MSA\nZCTA Population Estimates",
legend = "Population")
Run Code Online (Sandbox Code Playgroud)
我们还可以绘制整个MSA级别的美国地图,而不仅仅是放大特定的MSA吗?像这样的方法
zip_choropleth(df_pop_zip, legend = "Population")
Run Code Online (Sandbox Code Playgroud)
没有用,也可能会策划ZCTA地区,而不是MSA地区.
谢谢!
您可以使用state_zoom的参数zip_choropleth。但正如包文档中所述,没有基于 MSA 的分区统计图。其外观示例:
states <- unique( zip.regions$state.name)
lower48 <- states[ ! states %in% c('alaska','hawaii') ]
zip_choropleth(df_pop_zip,
state_zoom = lower48 ,
title = "2012 MSA\nZCTA Population Estimates",
legend = "Population")
Run Code Online (Sandbox Code Playgroud)
它看起来大部分是灰色的,因为 ZCTA 边界以灰色渲染,并且在这个比例下它们很密集。如果运行代码并查看更高的分辨率,您可以看到更多的填充。
我为您的任务推荐的另一种选择是tidycensus包。请参阅下面的代码片段,我相信它会创建一个与您感兴趣的内容类似的地图。我只选择了几个州来澄清视觉效果,并在县级绘制。我还只绘制了按总人口数排名前 85% 的 MSA。例如,这消除了丹维尔弗吉尼亚州。
# adapted from https://walkerke.github.io/2017/06/comparing-metros/
library(viridis)
library(ggplot2)
library(tidycensus)
library(tidyverse)
library(tigris)
library(sf)
options(tigris_class = "sf")
options(tigris_use_cache = TRUE)
# census_api_key("YOUR KEY HERE")
acs_var <- 'B01003_001E'
tot <- get_acs(geography = "county", variables = acs_var, state=c("PA", "VA", "DC","MD"),
geometry = TRUE)
head(tot)
metros <- core_based_statistical_areas(cb = TRUE) %>%
select(metro_name = NAME)
wc_tot <- st_join(tot, metros, join = st_within,
left = FALSE)
pct85 <- wc_tot %>% group_by(metro_name) %>%
summarise(tot_pop=sum(estimate)) %>% summarise(pct85 = quantile(tot_pop, c(0.85)))
pct85_msas = wc_tot %>% group_by(metro_name) %>%
summarise(tot_pop=sum(estimate)) %>% filter(tot_pop > pct85$pct85[1])
head(wc_tot)
ggplot(filter(wc_tot, metro_name %in% pct85_msas$metro_name),
aes(fill = estimate, color = estimate)) +
geom_sf() +
coord_sf(crs=3857) +
#facet_wrap(~metro_name, scales = "free", nrow = 1) +
theme_minimal() +
theme(aspect.ratio = 1) +
scale_fill_viridis() +
scale_color_viridis()
Run Code Online (Sandbox Code Playgroud)
结果图:
我注释掉的方面线似乎是 ggplot 中积极开发的区域。我遇到了一个错误,但我提到的源文章展示了如何充分利用它来为每个 MSA 显示一个面板,这很有意义。请参阅https://github.com/tidyverse/ggplot2/issues/2651。