【问题标题】:Mapping by US Census Divisions in ggplot2美国人口普查部门在 ggplot2 中的映射
【发布时间】:2021-01-08 00:41:00
【问题描述】:

我想按美国人口普查地区绘制数据。简而言之,我有州级数据,我使用 maps 包将其加入纬度/经度坐标。以下是生成的数据框的前 20 行:

 df_F4 %>% select(state, division, percap_rheum, group, long, lat) %>% print(n=20)
# A tibble: 15,539 x 6
# Groups:   division [9]
   state   division           percap_rheum group  long   lat
   <chr>   <chr>                     <dbl> <dbl> <dbl> <dbl>
 1 alabama east south central       70255.     1 -87.5  30.4
 2 alabama east south central       70255.     1 -87.5  30.4
 3 alabama east south central       70255.     1 -87.5  30.4
 4 alabama east south central       70255.     1 -87.5  30.3
 5 alabama east south central       70255.     1 -87.6  30.3
 6 alabama east south central       70255.     1 -87.6  30.3
 7 alabama east south central       70255.     1 -87.6  30.3
 8 alabama east south central       70255.     1 -87.6  30.3
 9 alabama east south central       70255.     1 -87.7  30.3
10 alabama east south central       70255.     1 -87.8  30.3
11 alabama east south central       70255.     1 -87.9  30.2
12 alabama east south central       70255.     1 -87.9  30.2
13 alabama east south central       70255.     1 -88.0  30.2
14 alabama east south central       70255.     1 -88.0  30.2
15 alabama east south central       70255.     1 -88.0  30.3
16 alabama east south central       70255.     1 -88.0  30.3
17 alabama east south central       70255.     1 -88.0  30.3
18 alabama east south central       70255.     1 -88.0  30.3
19 alabama east south central       70255.     1 -87.9  30.3
20 alabama east south central       70255.     1 -87.8  30.3
# … with 15,519 more rows

我想用 ggplot2 绘制它,按人口普查区域组织。我已经按地区汇总了数据,并可以在州一级绘制图表:

graph_theme <-  theme_light() + 
  theme(
    text = element_text(size=10),
    panel.grid.major.x = element_blank(), 
    panel.grid.minor = element_blank(), 
    plot.margin = unit(c(0.75, 0.25, 0.5, 0.5), "cm")) #top, R, bottom, L) 
  
map_theme <- theme(
    axis.title.x = element_blank(), 
    axis.title.y = element_blank(), 
    axis.text.x = element_blank(), 
    axis.text.y = element_blank(), 
    axis.ticks = element_blank(), 
    panel.grid = element_blank(), 
    legend.title = element_blank(), 
    legend.position = c(0.92, 0.25), # h / v 
    legend.background = element_blank()
)

df_F4 %>%
  ggplot(aes(
    long, 
    lat, 
    group = group)) +
  geom_polygon(aes(fill = percap_rheum), color = "white") + 
  scale_fill_viridis_c(labels = dollar_format(big.mark = ","), direction = -1) + 
  graph_theme + 
  map_theme

这张照片的结果是:

您可以看出那里有人口普查部门,但我想突出显示它们或以某种方式概述它们。任何建议将不胜感激!

【问题讨论】:

    标签: r ggplot2 maps geospatial


    【解决方案1】:

    一种方法是为每个分区创建一个多边形,然后将它们覆盖在状态数据上。

    使用来自tigris::state() 的数据的示例。步骤是:

    1. 将数据和过滤器下载到 CONUS,
    2. 创建分割多边形,
    3. 绘制覆盖了分区边界的状态数据。

    我还更改了地理 crs,这使美国看起来有点弯曲。不过不必这样做。

    library(tidyverse)
    library(tigris)
    library(sf)
    
    # Download state data and filter states
    sts <- states() %>%
      filter(!STUSPS %in% c('HI', 'AK', 'PR', 'GU', 'VI', 'AS', 'MP'))
    
    # Summarize to DIVISION polygons, see sf::st_union
    div <- sts %>%
      group_by(DIVISION) %>% 
      summarize()
    
    # Plot it
    ggplot() + 
      theme_void() +
      geom_sf(data = sts, 
              aes(fill = as.numeric(DIVISION)), 
              color = 'white') +
      geom_sf(data = div, 
              color = 'black', 
              fill = NA,
              size = 1) +
      scale_fill_viridis_c() +
      coord_sf(crs = 5070) +
      labs(fill = NULL)
      
    
    


    编辑:更新为使用 maps 包。找到有用的提示here

    # get data specifying which states are in which division
    div_dat <- states(cb = FALSE, resolution = '20m') %>%
      st_drop_geometry() %>%
      select(NAME, DIVISION) %>%
      mutate(ID = tolower(NAME))
    
    # get state data, convert to sf, join with division data
    states <- maps::map("state", plot = FALSE, fill = TRUE) %>%
      st_as_sf() %>%
      left_join(div_dat)
    
    # create division polygons
    div <- states %>%
      group_by(DIVISION) %>% 
      summarize()
    
    # plot it
    ggplot() + 
      theme_void() +
      geom_sf(data = states, 
              aes(fill = as.numeric(DIVISION)), 
              color = 'white') +
      geom_sf(data = div, 
              color = 'black', 
              fill = NA,
              size = 1) +
      scale_fill_viridis_c() +
      coord_sf(crs = 5070) +
      labs(fill = NULL)
    

    【讨论】:

    • 太棒了!由于某种原因,使用该方法的线条清晰度不能很好地打印出来。你知道我会如何使用“地图”包中的数据来做同样的事情吗?您可以通过以下方式获取数据:
    • install.packages("maps") 然后 maps::map_data("state")
    • 答案已更新。很好地捕捉到了低分辨率。我尝试使用 tigris::states 参数 resolution 但是,我无法让它看起来更好。
    • 无赖回复:分辨率,但很好的答案,谢谢你!真的很感激
    • 不客气!如果这回答了你的问题,你能接受这个答案吗? stackoverflow.com/help/someone-answers
    【解决方案2】:

    请向上面的@nniloc 寻求答案。如果有其他人跟进,我们使用 geom_sf 解决了一些问题。我尝试了一段时间让 geom_polygon 和“地图”包数据工作。这只是更高的分辨率,因为我认为有更多的数据点。

    无论如何,我最终达成了妥协。不能弯曲地图,但州线本身看起来不错。简而言之,我在之前创建的地图上使用了上述划分叠加层。它看起来足够好,我想如果它可以帮助别人,我会分享。

    
    # get data specifying which states are in which division
    div_dat <- states(cb = FALSE, resolution = '20m') %>%
      st_drop_geometry() %>%
      select(NAME, DIVISION) %>%
      mutate(ID = tolower(NAME))
    
    # get state data, convert to sf, join with division data
    states <- maps::map("state", plot = FALSE, fill = TRUE) %>%
      st_as_sf() %>%
      left_join(div_dat)
    
    # create division polygons
    div <- states %>%
      group_by(DIVISION) %>% 
      summarize()
    
    # Plot percapita spending 
    
    ggplot() + 
      graph_theme + 
      map_theme + 
      geom_polygon(data = df_F4, 
                   aes(long, lat, group = group, fill = percap_rheum), 
                   color = "white") +
      geom_sf(data = div, 
              color = "#838383", 
              fill = NA,
              size = 1) + 
      scale_fill_viridis_c(labels = dollar_format(big.mark = ","), direction = -1) 
    
    

    【讨论】:

      猜你喜欢
      • 2012-01-24
      • 2018-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-22
      • 2021-10-10
      相关资源
      最近更新 更多