You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

阿拉斯加州部分县US地图无数据显示问题求助

问题描述

用R语言的usmap包绘制阿拉斯加州各郡2022年成人肥胖率地图时,部分郡显示为灰色(无对应数据)。即使调用usmap内置的人口数据,这些郡依然呈灰色。推测是FIPS编码匹配问题,但无法确定这些灰色郡的FIPS编码及具体异常原因。

相关代码如下:

# 读取数据
chd_map = read.csv(file = 
'https://www.countyhealthrankings.org/sites/default/files/media/document/analytic_data2022.csv', 
header = TRUE, sep=",")

# 提取FIPS编码和肥胖率数据
chd_map = chd_map[-c(1,2),c(3,75)]

# 将肥胖率转为数值型并放大100倍(转为百分比)
chd_map$Adult.obesity.raw.value = as.numeric(chd_map$Adult.obesity.raw.value) * 100

# 重命名FIPS列
colnames(chd_map)[1] = 'fips'

# 绘制地图
plot_usmap(
  data = chd_map,
  size = 0.1,
  regions = "counties",
  include = c('AK'),
  values = "Adult.obesity.raw.value",
  color = "black") +
  scale_fill_continuous(type = "viridis", name = "2022年成人肥胖率(百分比)", label = scales::comma
  ) +
  labs(title = "图X:阿拉斯加州各郡成人肥胖率分布") + 
  theme(panel.background = element_rect(color = "white", fill = "white"), legend.position = "bottom")

问题截图:
阿拉斯加州肥胖率地图(含灰色无数据郡)

解决方案

核心原因

阿拉斯加州的部分“郡”在统计上属于人口普查区(Census Area),而非标准郡(County),但usmap的plot_usmap函数默认会加载这些普查区的地理信息,而你使用的County Health Rankings数据里没有包含这些普查区的记录,导致匹配失败显示灰色。

解决步骤

  1. 找出灰色区域的FIPS编码
    用usmap内置的countypop数据集查看阿拉斯加的全部统计区域:
library(usmap)
ak_regions <- countypop[countypop$abbr == "AK", c("fips", "county")]
print(ak_regions)

对比你导入的chd_map数据,用以下代码找出缺失的FIPS编码:

missing_fips <- setdiff(ak_regions$fips, chd_map$fips)
print(missing_fips)

输出结果就是地图上灰色区域的编码,对应的数据在你用的CSV里不存在。

  1. 修复FIPS编码格式
    确保你的fips列是字符型,避免前导零丢失(阿拉斯加FIPS以02开头,比如02013,数值型会自动去掉前导零导致匹配失败):
chd_map$fips <- as.character(chd_map$fips)
  1. 两种显示方案任选其一
  • 方案一:只显示有数据的区域
    修改plot_usmap的include参数,只保留你数据里存在的阿拉斯加区域:
plot_usmap(
  data = chd_map,
  size = 0.1,
  regions = "counties",
  include = chd_map$fips[substr(chd_map$fips, 1, 2) == "02"],
  values = "Adult.obesity.raw.value",
  color = "black") +
  scale_fill_continuous(type = "viridis", name = "2022年成人肥胖率(百分比)", label = scales::comma) +
  labs(title = "图X:阿拉斯加州各郡成人肥胖率分布") + 
  theme(panel.background = element_rect(color = "white", fill = "white"), legend.position = "bottom")
  • 方案二:补全缺失区域数据
    如果需要完整显示所有区域,你可以:
    • 查找包含阿拉斯加普查区的肥胖率数据源
    • 用阿拉斯加州平均肥胖率填充缺失值:
      ak_avg <- mean(chd_map$Adult.obesity.raw.value, na.rm = TRUE)
      missing_rows <- data.frame(
        fips = missing_fips,
        Adult.obesity.raw.value = ak_avg
      )
      chd_map_full <- rbind(chd_map, missing_rows)
      
      再用chd_map_full作为数据源绘制地图即可。

内容的提问来源于stack exchange,提问作者Ben Allen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 07:05:23