如何在sf格式的动物GPS数据上使用ggplot2的geom_path绘制轨迹?
动物GPS轨迹绘制问题解决方案
核心问题原因
geom_path() 无法直接识别sf对象的geometry列,需要明确指定x/y坐标映射,或转换sf对象为线类型后用sf专属图层绘制。
方法1:直接使用保留的经纬度列绘制轨迹
你的sf对象已保留Lat和Lon列,直接给geom_path()指定x/y美学即可(数据需按时间顺序排列,你的数据已满足):
ggplot()+ # 绘制带时间配色的点位 geom_sf(data = gps_6067.sf, size = 1.5, aes(color = DT_UTC))+ scale_color_datetime(low = 'orange', high = 'green')+ # 添加轨迹线,指定x/y坐标 geom_path(data = gps_6067.sf, aes(x = Lon, y = Lat), color = "gray50", linewidth = 0.8)
方法2:将sf点对象转为线对象绘制轨迹
完全基于sf工作流,把按时间排序的点合并为线类型sf对象,再用geom_sf()绘制:
# 按时间排序后将点合并为线 gps_6067_line.sf <- gps_6067.sf %>% arrange(DT_UTC) %>% st_combine() %>% st_cast("LINESTRING") # 叠加轨迹线和点位 ggplot()+ geom_sf(data = gps_6067_line.sf, color = "gray50", linewidth = 0.8)+ geom_sf(data = gps_6067.sf, size = 1.5, aes(color = DT_UTC))+ scale_color_datetime(low = 'orange', high = 'green')
CRS统一处理
叠加栅格等底图时,必须确保所有空间数据CRS一致:
- 查看底图CRS:
st_crs(你的底图对象) - 转换sf对象CRS匹配底图:
gps_6067.sf <- st_transform(gps_6067.sf, st_crs(你的底图对象))
内容的提问来源于stack exchange,提问作者irap
相关产品推荐
相关产品推荐

