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

R中tmap忽略breaks参数,自定义离散分桶不生效

tmap自定义分桶不生效的解决方法

关键修复点

tmap识别自定义分桶的核心是必须显式设置style = "fixed",你之前的代码只是注释掉了这个参数,这是分桶规则被忽略的主要原因。同时你的breaks向量(6个值)和调色板(5个颜色)的长度匹配是正确的,符合离散分桶的要求。

修改后的可运行代码

robin <- "+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"

map <- tm_shape(mapdata, projection = robin, bbox = boundaries) +
  tm_polygons(
    "Sum.of.increase", 
    style = "fixed",  # 启用自定义分桶的必备参数
    breaks = c(-Inf, 0, .5, 1, 2, Inf),
    palette = c("white","#FF7C80", "wheat1", "#ADDC9C", "#6AC04A"), 
    colorNA = "grey",
    legend.show = TRUE
  ) + 
  tm_layout(frame = FALSE, bg.color = "transparent") 

print(map)  # 显式调用print确保地图正确渲染

额外排查建议

  • 确认Sum.of.increase列是数值型:如果是因子类型,先转换为数值型,否则分桶规则无法生效。转换代码:
    mapdata$Sum.of.increase <- as.numeric(as.character(mapdata$Sum.of.increase))
    
  • 不需要额外设置tmap_options(max.categories = 5),自定义分桶模式下这个参数不会影响结果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 05:23:14