县级小麦产量分布图州间存在间隙问题求助
解决县级地图间隙问题的方法
核心问题:分组逻辑错误
你当前代码里把group设为state,这会导致同一州内的所有县被当作一个多边形来绘制,自然会出现间隙。正确的做法应该按县级单元分组,比如group = fips或者group = county(取决于你的数据集列名),这样每个县的多边形才能独立正确渲染。
修改后的代码示例
library(maps) library(urbnmapr) library(dplyr) library(ggplot2) g43 <- wheat_corn_insurance1 %>% filter(year == 1989, wheat == 1) %>% ggplot(aes(x = longitude, y = latitude, group = fips, fill = stateproduction/1000)) + geom_polygon(color = NA) + scale_fill_gradient(low = "white", high = "red") + labs(title = "County-Level Choropleth Map", subtitle = "Wheat Production (thousand bu), 1989") g43
额外排查点
- 检查你的合并数据集是否包含所有县级FIPS码,有没有遗漏的县,缺失的单元会导致地图出现空白间隙。
- 确认经纬度数据的精度是否足够,部分低精度的地理坐标也可能导致多边形衔接出现缝隙。
- 如果用
urbnmapr,可以直接用它自带的counties数据集匹配你的产量数据,避免自行合并坐标可能出现的错误:# 示例:用urbnmapr自带数据集匹配 merged_data <- left_join(counties, wheat_corn_insurance1 %>% filter(year == 1989, wheat == 1), by = c("fips" = "你的FIPS列名")) ggplot(merged_data, aes(x = long, y = lat, group = group, fill = stateproduction/1000)) + geom_polygon(color = NA) + scale_fill_gradient(low = "white", high = "red") + labs(title = "County-Level Choropleth Map", subtitle = "Wheat Production (thousand bu), 1989")
内容的提问来源于stack exchange,提问作者Homa Taheri
相关产品推荐
相关产品推荐

