我的问题是通过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")我们可以绘制整个MSA级别的美国地图,而不是只有一个zoom到特定的MSA吗?一种方法,比如
zip_choropleth(df_pop_zip, legend = "Population")没有工作,也可能会绘制ZCTA区域,而不是MSA区域。
谢谢!
发布于 2018-09-06 04:21:39
您可以对zip_choropleth使用state_zoom参数。但是,正如在软件包文档中所指出的,没有基于MSA的choropleth。下面是一个这样的例子:
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")

它看起来大部分是灰色的,因为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()结果图:

我注释掉的方面线似乎是ggplot中一个活跃的开发领域。我收到了一个错误,但我提到的source article展示了如何很好地利用它来显示每个MSA的一个面板,这很有意义。参见https://github.com/tidyverse/ggplot2/issues/2651。
https://stackoverflow.com/questions/52150767
复制相似问题