如何在ggplot2中调整莫尔韦德投影下X轴刻度间距?
解决ggplot2莫尔韦德投影下X轴刻度不均匀及添加经纬线问题
问题背景
使用以下ggplot2代码绘制莫尔韦德投影的地震分布时,X轴刻度间距不均匀,无法匹配目标图的均匀刻度+经纬线效果:
ggplot(data=earthquake,aes(longitude,latitude))+ geom_point(data=earthquake,aes(longitude,latitude),alpha=0.5,col="red",fill="red",shape=21)+ coord_map(projection = "mollweide",xlim=c(-180,180))+ xlab("Galactic Longitude")+ ylab("Galactic Latitude")+ scale_x_continuous(breaks=seq(-180,180,60),limits=c(-180,180))+ scale_y_continuous(breaks=seq(-90,90,30))
其中earthquake是包含longitude(范围-180至180)和latitude(范围-90至90)列的数据对象。
解决方案
核心是替换coord_map为更精准的coord_sf,并手动添加经纬线:
完整代码
library(ggplot2) library(sf) # 生成经纬线数据 graticule <- st_graticule( x = seq(-180, 180, 60), y = seq(-90, 90, 30), crs = st_crs(4326) ) ggplot() + # 添加经纬线 geom_sf(data = graticule, color = "gray80", linetype = "dashed") + # 添加地震点(继承全局映射) geom_point(data = earthquake, aes(longitude, latitude), alpha = 0.5, col = "red", fill = "red", shape = 21) + # 指定莫尔韦德投影(EPSG:4055) coord_sf(crs = st_crs(4055)) + # 设置轴标签与刻度 labs(x = "Galactic Longitude", y = "Galactic Latitude") + scale_x_continuous(breaks = seq(-180, 180, 60)) + scale_y_continuous(breaks = seq(-90, 90, 30))
关键说明
coord_sf(crs = st_crs(4055)):莫尔韦德投影的EPSG代码为4055,coord_sf能更精准地处理投影后的刻度布局,解决coord_map的刻度偏移问题。st_graticule():生成WGS84坐标系(EPSG:4326)下的经纬线,确保投影后线条正确显示。- 图层顺序:先添加经纬线,再添加点,避免点被线条遮挡。
内容的提问来源于stack exchange,提问作者Fisherman's Friend
相关产品推荐
相关产品推荐

