使用 tigris 与 tidycensus 的制图流程

在 R 中分析美国人口普查数据

Kyle Walker

Instructor

教育点图

在 R 中分析美国人口普查数据

使用 sf 生成随机点

sf 中用于随机点生成的关键函数:st_sample()

dc_dots <- map(c("White", "Black", "Hispanic", "Asian"), function(group) {
  dc_race %>%
    filter(variable == group) %>%
    st_sample(., size = .$value / 100) %>%
    st_sf() %>%
    mutate(group = group) 
}) %>%
  reduce(rbind)
在 R 中分析美国人口普查数据

随机点生成的考虑事项

更快的绘图:

dc_dots <- dc_dots %>%
  group_by(group) %>%
  summarize()

更准确的可视化:

dc_dots_shuffle <- sample_frac(dc_dots, size = 1)
在 R 中分析美国人口普查数据

使用 sf 的基础点密度制图

plot(dc_dots_shuffle, key.pos = 1)

基础点密度图

在 R 中分析美国人口普查数据

tigris 的辅助数据

options(tigris_class = "sf")

dc_roads <- roads("DC", "District of Columbia") %>%
  filter(RTTYP %in% c("I", "S", "U"))

dc_water <- area_water("DC", "District of Columbia")
dc_boundary <- counties("DC", cb = TRUE)
在 R 中分析美国人口普查数据

tigris 的辅助数据

plot(dc_water$geometry, col = "lightblue")

DC 水域图

在 R 中分析美国人口普查数据

使用 ggplot2 的点密度制图

ggplot() + 
  geom_sf(data = dc_boundary, color = NA, fill = "white") + 
  geom_sf(data = dc_dots, aes(color = group, fill = group), size = 0.1) + 
  geom_sf(data = dc_water, color = "lightblue", fill = "lightblue") + 
  geom_sf(data = dc_roads, color = "grey") + 
  coord_sf(crs = 26918, datum = NA) + 
  scale_color_brewer(palette = "Set1", guide = FALSE) +
  scale_fill_brewer(palette = "Set1") +
  labs(title = "The racial geography of Washington, DC", 
       subtitle = "2010 decennial U.S. Census", 
       fill = "", 
       caption = "1 dot = approximately 100 people.\nData acquired with 
                  the R tidycensus and tigris packages.")
在 R 中分析美国人口普查数据

最终点密度图

在 R 中分析美国人口普查数据

点密度制图的注意事项

  • 注意点密度图的潜在误读
  • 谨慎选择定性配色
  • 精选辅助图层
在 R 中分析美国人口普查数据

让我们练习吧!

在 R 中分析美国人口普查数据

Preparing Video For Download...