【问题标题】:如何仅使用邮政编码在地图上创建热图 [关闭]
【发布时间】:2022-01-23 15:22:09
【问题描述】:

我有下面的数据框,我想知道是否可以仅使用邮政编码在芝加哥地图上绘制热图。 Count 将用作数值。

structure(list(Zip = structure(c(15L, 19L, 25L, 27L, 30L, 30L, 
41L), .Label = c("60411", "60415", "60462", "60607", "60608", 
"60609", "60610", "60612", "60613", "60614", "60615", "60616", 
"60617", "60618", "60619", "60620", "60621", "60622", "60623", 
"60624", "60625", "60626", "60628", "60629", "60630", "60631", 
"60632", "60633", "60634", "60636", "60637", "60638", "60639", 
"60640", "60641", "60642", "60643", "60644", "60645", "60646", 
"60647", "60649", "60651", "60652", "60653", "60655", "60656", 
"60657", "60659", "60660", "60707"), class = "factor"), Count = c(2L, 
2L, 2L, 2L, 2L, 2L, 2L)), class = c("grouped_df", "tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -7L), groups = structure(list(
    Zip = structure(c(15L, 19L, 25L, 27L, 30L, 41L), .Label = c("60411", 
    "60415", "60462", "60607", "60608", "60609", "60610", "60612", 
    "60613", "60614", "60615", "60616", "60617", "60618", "60619", 
    "60620", "60621", "60622", "60623", "60624", "60625", "60626", 
    "60628", "60629", "60630", "60631", "60632", "60633", "60634", 
    "60636", "60637", "60638", "60639", "60640", "60641", "60642", 
    "60643", "60644", "60645", "60646", "60647", "60649", "60651", 
    "60652", "60653", "60655", "60656", "60657", "60659", "60660", 
    "60707"), class = "factor"), .rows = structure(list(1L, 2L,3L, 4L, 5:6, 7L), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -6L), .drop = TRUE)) 

【问题讨论】:

    标签: r dictionary


    【解决方案1】:

    您需要邮政编码和地理空间边界之间的映射。这是一个选项:

    1. Download shape file for Chicago ZIP boundaries。保存文件并解压;我假设相关文件位于您的 R 工作文件夹的根目录中。

    2. 假设您的data.frame 带有计数和邮政编码称为data,请读取形状文件并通过左连接添加计数。

      library(rgdal)
      # Make sure the name of the shape file matches the name of the shape file 
      # from the ZIP archive
      shp <- readOGR("geo_export_4e602fd1-be14-4590-8a68-fdbca198af8f.shp")
      
      # Add count data
      library(dplyr)
      shp@data <- shp@data %>% left_join(data, by = c("zip" = "Zip"))
      
    3. 使用leaflet 的示例图。

      library(leaflet)
      pal <- colorNumeric("Reds", domain = NULL)
      leaflet(shp) %>%
          addPolygons(
              color = "black", 
              weight = 1, 
              fillColor = ~ pal(Count))
      


    更新

    要添加图例,我建议先从Count 列中删除NAs。

    library(dplyr)
    library(tidyr)
    shp@data <- shp@data %>% 
        left_join(data, by = c("zip" = "Zip")) %>%
        replace_na(list(Count = 0))
    

    然后使用addLegend添加颜色图例

    library(leaflet)
    pal <- colorNumeric("Reds", domain = NULL)
    leaflet(shp) %>%
        addPolygons(
            color = "black", 
            weight = 1, 
            fillColor = ~ pal(Count)) %>%
        addLegend(
            "bottomright", 
            pal = pal, 
            values = ~ Count,
            title = "Count",
            opacity = 1)
    

    【讨论】:

    • 简单明了的谢谢。传说也有可能吗?我已经添加了 %>% addLegend(position = "bottomright", pal = pal, values = ~Count ),但根据我的数据,我并没有完全得到我想要的
    • @firmo23 请参阅我对如何添加图例的编辑。关键是先用零替换NAs。
    猜你喜欢
    • 2017-01-27
    • 2021-11-14
    • 2020-04-19
    • 2018-05-16
    • 1970-01-01
    • 2012-01-25
    • 2015-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多