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

如何在ggmap中去除灰色背景并保留自定义坐标轴刻度?

问题

我用以下代码构建ggmap,同时添加多边形和点数据,问题出在坐标轴范围上:

ggMFI <- CY_map +   
  geom_point( data = coords_CY, aes(x = lon, y = lat), col = "black", size =  1.5   )       +
  geom_sf(data = StudyArea_shp, colour = "white", inherit.aes = FALSE, show.legend = FALSE) +
  geom_point( data = df_PREC,   aes(x = lon, y = lat, col = dMFI),    size =  2.5   )       +
  #xlab("Longitude (°E)") + ylab("Latitude (°S)" )                                           +
  scale_x_continuous(breaks = c(32.5, 33, 33.5, 34.0)) +
  scale_y_continuous(breaks = c(34.6, 34.8, 35.0, 35.2)) +
  theme(axis.text.x  = element_text(size = 30),axis.text.y  = element_text(size = 30) )     +
  theme(axis.title.x = element_blank(),axis.title.y = element_blank() )         +
  theme(legend.title=element_text(size=20),legend.text=element_text(size=20), 
       legend.position = c(0.94, 0.30))  +   
  scale_color_gradientn(colours = c('#5749a0', '#0f7ab0', '#00bbb1',
                                    '#bef0b0', '#fdf4af', '#f9b64b',
                                    '#ec840e', '#ca443d', '#a51a49'))

当前问题:

  • 保留scale_x_continuous和scale_y_continuous时,图表出现灰色背景,地图无法贴合坐标轴
  • 移除这两个scale后,灰色背景消失,地图贴合坐标轴,但无法自定义刻度

我需要同时实现:去除灰色背景、地图贴合坐标轴、保留自定义刻度。

解决方案

问题根源是ggmap默认坐标系与geom_sf坐标系冲突,自定义scale会超出地图范围导致灰色背景,可通过以下两种方法解决:

方法1:在scale中指定与地图匹配的范围

先获取CY_map的坐标轴范围,将其传入scale_x_continuous和scale_y_continuous的limits参数,确保刻度范围与地图完全一致:

# 获取CY_map的坐标范围
x_limits <- CY_map$coordinates$x[c(1,2)]
y_limits <- CY_map$coordinates$y[c(1,2)]

# 修改后的代码
ggMFI <- CY_map +   
  geom_point( data = coords_CY, aes(x = lon, y = lat), col = "black", size =  1.5   )       +
  geom_sf(data = StudyArea_shp, colour = "white", inherit.aes = FALSE, show.legend = FALSE) +
  geom_point( data = df_PREC,   aes(x = lon, y = lat, col = dMFI),    size =  2.5   )       +
  scale_x_continuous(breaks = c(32.5, 33, 33.5, 34.0), limits = x_limits) +
  scale_y_continuous(breaks = c(34.6, 34.8, 35.0, 35.2), limits = y_limits) +
  theme(axis.text.x  = element_text(size = 30),axis.text.y  = element_text(size = 30) )     +
  theme(axis.title.x = element_blank(),axis.title.y = element_blank() )         +
  theme(legend.title=element_text(size=20),legend.text=element_text(size=20), 
       legend.position = c(0.94, 0.30))  +   
  scale_color_gradientn(colours = c('#5749a0', '#0f7ab0', '#00bbb1',
                                    '#bef0b0', '#fdf4af', '#f9b64b',
                                    '#ec840e', '#ca443d', '#a51a49'))

方法2:使用coord_sf统一坐标系并设置刻度

因用到geom_sf,改用coord_sf控制坐标轴刻度和范围,同时保证地图贴合坐标轴:

ggMFI <- CY_map +   
  geom_point( data = coords_CY, aes(x = lon, y = lat), col = "black", size =  1.5   )       +
  geom_sf(data = StudyArea_shp, colour = "white", inherit.aes = FALSE, show.legend = FALSE) +
  geom_point( data = df_PREC,   aes(x = lon, y = lat, col = dMFI),    size =  2.5   )       +
  coord_sf(
    xlim = CY_map$coordinates$x[c(1,2)],
    ylim = CY_map$coordinates$y[c(1,2)],
    expand = FALSE, # 禁止扩展范围,确保贴合坐标轴
    crs = 4326 # 匹配ggmap默认的WGS84坐标系,若StudyArea_shp坐标系不同需调整
  ) +
  scale_x_continuous(breaks = c(32.5, 33, 33.5, 34.0)) +
  scale_y_continuous(breaks = c(34.6, 34.8, 35.0, 35.2)) +
  theme(axis.text.x  = element_text(size = 30),axis.text.y  = element_text(size = 30) )     +
  theme(axis.title.x = element_blank(),axis.title.y = element_blank() )         +
  theme(legend.title=element_text(size=20),legend.text=element_text(size=20), 
       legend.position = c(0.94, 0.30))  +   
  scale_color_gradientn(colours = c('#5749a0', '#0f7ab0', '#00bbb1',
                                    '#bef0b0', '#fdf4af', '#f9b64b',
                                    '#ec840e', '#ca443d', '#a51a49'))

内容的提问来源于stack exchange,提问作者ΚΩΝΣΤΑΝΤΙΝΟΣ ΠΑΝΑΓΙΩΤΟΥ

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 09:37:08