使用geom_sf绘制sf数据时如何消除空白与异常线条?
问题解决思路与代码调整
问题原因分析
- 初始仅设置
col = test时,仅绘制了网格正方形的边框,内部无填充,因此呈现棋盘状空白区域。 - 添加
fill后出现的规则线条,是相邻网格的边框重复叠加导致的:每个独立的sf多边形都自带边框,密集排列时相邻边框重叠渲染,视觉上形成深色的多余线条,这和网格的密集设置直接相关。
解决方案
方案1:仅保留填充色(推荐)
如果不需要显示网格边框,直接隐藏边框,只通过填充色展示test值,彻底解决边框叠加问题:
pred_plot <- ggplot(polyg_sf) + geom_sf(aes(fill = test), col = NA) + # 隐藏边框 coord_sf(xlim = c(-6,0), ylim = c(43,48), expand = FALSE)+ scale_fill_distiller(palette = "Spectral")+ labs(caption= "k = 0.75")+ xlab("Longitude (deg)")+ ylab("Latitude (deg)")+ facet_wrap(. ~ Month)
方案2:保留边框但弱化叠加效果
如果必须保留边框,通过缩小边框尺寸、让边框色与填充色匹配,减少叠加的视觉影响:
pred_plot <- ggplot(polyg_sf) + geom_sf(aes(fill = test, col = test), size = 0.05) + # 极小化边框尺寸 coord_sf(xlim = c(-6,0), ylim = c(43,48), expand = FALSE)+ scale_color_distiller(palette = "Spectral") + scale_fill_distiller(palette = "Spectral")+ labs(caption= "k = 0.75")+ xlab("Longitude (deg)")+ ylab("Latitude (deg)")+ guides(col=guide_colorbar(ticks.colour = NA))+ facet_wrap(. ~ Month)
内容的提问来源于stack exchange,提问作者JulietteTC
相关产品推荐
相关产品推荐

