r - chorolethr:绘制 MSA 级别的 map ?

标签 r plot census choroplethr

我的问题涉及通过 choroplethr 在 MSA 级别绘制整个美国 map 和 choroplethrZip .

在下面的示例中,我们绘制了 1) 在美国县级 map 上的人口普查人口信息和 2) 所选大都市/小都市统计区 (MSA) 级别的缩放 map 。

示例 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")

而不是只有 zoom进入特定的 MSA,我们是否还可以绘制整个 MSA 级别的美国 map ?一种方法,如
zip_choropleth(df_pop_zip, legend = "Population")

不起作用,并且还可能绘制 ZCTA 区域,而不是 MSA 区域。

谢谢!

最佳答案

您可以使用 state_zoom论据 zip_choropleth .但正如包文件中所述,没有基于 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")

R plot of US population

它看起来大部分是灰色的,因为 ZCTA 边界呈现为灰色,并且在这个比例下它们很密集。如果您运行代码并查看更高的分辨率,您可以看到更多的填充。

我为您的任务推荐的替代方法是 tidycensus包裹。请参阅下面的代码片段,我相信它会创建一个与您感兴趣的 map 类似的 map 。我只选择了几个州来阐明视觉效果,并在县级进行绘制。我也只绘制了总人口中前 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()

结果图:

enter image description here

我注释掉的方面似乎是 ggplot 中活跃开发的一个领域。我遇到了错误,但是 source article我提到的展示了如何充分利用每个 MSA 显示一个面板,这很有意义。见 https://github.com/tidyverse/ggplot2/issues/2651 .

关于r - chorolethr:绘制 MSA 级别的 map ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52150767/

相关文章:

c++ - 当我期望 R 中的数据帧输出时,为什么 Rccp 返回类似列表的输出?

python - 将轴尺寸限制为另一个轴的尺寸

c++ - 如何计算两个short int的汉明距离?

python - 如何修改seaborn fiddle 图例

r - 如何在 R 中绘制 3D 条形图

sql - 时间点的最近邻

即使可以从终端运行相同的命令,R system() 也无法分配内存

r - 如何在 ggplotly 中为条形图更改悬停背景颜色

r - 从数据框中查找到特定位置最近的城市