ggplot2 geom_sf坐标轴显示米制单位而非经纬度标签的方法
问题描述
持有以米为计量单位的投影CRS对应的sf空间对象,使用ggplot2的geom_sf绘图时,希望坐标轴标签与原始几何对象的坐标单位保持一致,展示对应CRS下的米制坐标值,而非默认生成的经纬度标签,需要移除默认的经纬度轴标签。
复现示例
library(ggplot2) library(sf) data(meuse, package="sp") meuse_sf = st_as_sf(meuse, coords = c("x", "y"), crs = 28992) # Simple feature collection with 155 features and 12 fields # Geometry type: POINT # Dimension: XY # Bounding box: xmin: 178605 ymin: 329714 xmax: 181390 ymax: 333611 # Projected CRS: Amersfoort / RD New # First 3 features: # cadmium copper lead zinc elev dist om ffreq soil lime landuse dist.m geometry # 1 11.7 85 299 1022 7.909 0.00135803 13.6 1 1 1 Ah 50 POINT (181072 333611) # 2 8.6 81 277 1141 6.983 0.01222430 14.0 1 1 1 Ah 30 POINT (181025 333558) # 3 6.5 68 199 640 7.800 0.10302900 13.0 1 1 1 Ah 150 POINT (181165 333537) st_crs(meuse_sf)$units # [1] "m" ggplot() + geom_sf(data = meuse_sf, aes(color = cadmium))
示例运行效果:
解决方案
出现该问题的原因是geom_sf默认会自动适配坐标标注逻辑,未显式指定绘图坐标系时可能触发地理坐标(经纬度)标注规则,只需在绘图代码中添加coord_sf()显式绑定数据本身的投影坐标系,同时关闭经纬度专用标注即可:
ggplot() + geom_sf(data = meuse_sf, aes(color = cadmium)) + # 显式指定使用数据本身的米制投影CRS,移除经纬度格式标注 coord_sf(crs = st_crs(meuse_sf), datum = NULL)
参数说明:
crs = st_crs(meuse_sf):强制绘图使用与输入数据完全一致的投影坐标系,不会自动转换为WGS84等地理坐标系展示经纬度datum = NULL:移除默认的经纬度专属标识(度符号、南北/东西方位后缀),直接输出纯数值的米制坐标刻度
如果需要自定义坐标名称、刻度间隔,可以搭配常规的连续标度函数调整:
ggplot() + geom_sf(data = meuse_sf, aes(color = cadmium)) + coord_sf(crs = st_crs(meuse_sf), datum = NULL) + scale_x_continuous(name = "X坐标(m)", breaks = seq(178600, 181400, 500)) + scale_y_continuous(name = "Y坐标(m)", breaks = seq(329700, 333700, 500))
内容的提问来源于stack exchange,提问作者chamaoskurumi
相关产品推荐
相关产品推荐

