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

choroplethr县级地图添加边框时报object 'value' not found错误

报错原因

Error in FUN(X[[i]], ...) : object 'value' not found 报错来自最后叠加的geom_polygon()层:

  • 参考的旧适配方案是针对早期版本choroplethr编写的,包版本更新后,county_choropleth()内部会对传入的原始数据做处理,和地图边界数据合并、对数值做分箱后才会送入ggplot绘图,最终绘图环境里不存在原始数据里的value列。
  • 新增的geom_polygon里写了aes(fill=value),要求图层把value列映射给填充属性,但图层找不到对应列,直接触发报错。
  • 原代码还有一处无效参数:labs(legend = "DV/SV Incidents (total)")不会生效,ggplot2中填充图例的标题对应参数名是fill。
修复方案

不需要在新增的多边形层重新做填充映射——county_choropleth已经完成了所有填充色的绘制,只需要叠加一层无填充、仅黑色描边的多边形层,继承已有图层的坐标映射即可,不需要额外加载边界数据。
修复后的完整可运行代码如下:

library(choroplethr)
library(choroplethrMaps)
library(ggplot2)

# 原始数据集保持不变
tni_data <- structure(list(county = c("atlantic", "bergen", "burlington", 
"camden", "cape may", "cumberland", "essex", "gloucester", "hudson", 
"hunterdon", "mercer", "middlesex", "monmouth", "morris", "ocean", 
"passaic", "salem", "somerset", "sussex", "union", "warren"), 
    value = c(3187, 3410, 3747, 6532, 1339, 2510, 6420, 2417, 
    2656, 570, 2139, 4336, 3514, 1802, 4372, 2451, 773, 1840, 
    1265, 3054, 1311), region = c(34001, 34003, 34005, 34007, 
    34009, 34011, 34013, 34015, 34017, 34019, 34021, 34023, 34025, 
    34027, 34029, 34031, 34033, 34035, 34037, 34039, 34041), 
    county.fips.character = c("34001", "34003", "34005", "34007", 
    "34009", "34011", "34013", "34015", "34017", "34019", "34021", 
    "34023", "34025", "34027", "34029", "34031", "34033", "34035", 
    "34037", "34039", "34041"), state.name = c("new jersey", 
    "new jersey", "new jersey", "new jersey", "new jersey", "new jersey", 
    "new jersey", "new jersey", "new jersey", "new jersey", "new jersey", 
    "new jersey", "new jersey", "new jersey", "new jersey", "new jersey", 
    "new jersey", "new jersey", "new jersey", "new jersey", "new jersey"
    ), state.fips.character = c("34", "34", "34", "34", "34", 
    "34", "34", "34", "34", "34", "34", "34", "34", "34", "34", 
    "34", "34", "34", "34", "34", "34"), state.abb = c("NJ", 
    "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", 
    "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ"
    ), tni.per.capita = c(0.0120870785451511, 0.00365800545375359, 
    0.00841362616734292, 0.0128970859140997, 0.0145481806625452, 
    0.0167862660255339, 0.00803529522200319, 0.00828772853831489, 
    0.00395008261562097, 0.00458306196782208, 0.00582151702365076, 
    0.00525536262729346, 0.00567877891708886, 0.00366375585804471, 
    0.00720042952241982, 0.00488416303659037, 0.0123907990702893, 
    0.00559382733314282, 0.00900432777176698, 0.00548943903109783, 
    0.0124540454273419)), class = "data.frame", row.names = c(NA, 
-21L))

county_choropleth(tni_data,
                  state_zoom = "new jersey",
                  reference_map = TRUE,
                  num_colors = 8) +
  scale_fill_brewer(palette="YlOrRd") +
  labs(title = "DV/SV Incidents by New Jersey County",
       fill = "DV/SV Incidents (total)") + 
  geom_polygon(color = "black", fill = NA, linewidth = 0.3)

参数说明

  • color = "black":设置多边形边框为黑色
  • fill = NA:设置多边形填充为完全透明,不会覆盖下方已经绘制好的分级填色
  • linewidth = 0.3:控制边框粗细,可根据显示需求调整数值,数值越大边框越粗

内容的提问来源于stack exchange,提问作者Sean Wallace

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.29 18:14:52