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

RStudio中绘制美国地图经纬度点时报st_cast.POINT错误如何解决

报错原因及解决方案

报错原因

该报错是因为geom_sf()的图层默认使用sf对象的投影坐标系,而你后续用geom_point()传入的是普通data.frame格式的经纬度数据,没有关闭ggplot的全局美学映射继承,程序会尝试将普通经纬度点强制转换为sf的几何类型,转换失败触发报错。

解决方法

有两种可行的修复方案,更推荐第一种,坐标匹配更稳定,不会出现偏移问题:

方案1:将经纬度数据转为sf点对象(推荐)

先把你的经纬度数据表转换为和底图同坐标系的sf对象,之后统一用geom_sf()绘制点图层:

# 转换经纬度表为sf点对象,坐标系和world底图保持一致
lat_lon_sf <- st_as_sf(lat_lon, coords = c("LON_DD83", "LAT_DD83"), crs = st_crs(world))

# 绘图代码
ggplot() +  
  geom_sf(data = world) +  
  geom_sf(data = lat_lon_sf, size = 4, shape = 23, fill = "darkred") +  
  coord_sf(xlim = c(-80, -65), ylim = c(40, 50), expand = FALSE)

方案2:关闭点图层的全局映射继承

如果不想修改数据格式,可以直接在geom_point()里添加inherit.aes = FALSE参数,断开和前面sf图层的映射继承关系:

ggplot(data = world) +  
  geom_sf() +  
  geom_point(data = lat_lon, aes(x = LON_DD83, y = LAT_DD83), 
             size = 4, shape = 23, fill = "darkred",
             inherit.aes = FALSE) +  
  coord_sf(xlim = c(-80, -65), ylim = c(40, 50), expand = FALSE)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 06:45:02