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

叠加ggmap的ggplot轴刻度未按预期显示问题求助

解决ggmap叠加ggplot时无法显示全部指定轴刻度的问题

你的问题根源很明确:指定的刻度值超出了坐标轴的范围限制:

  • breaks_x里的-11.2、-13.4、-15.6都小于x轴下限xmin=-8.93,不在设置的limits = c(xmin, xmax)范围内
  • breaks_y里的46.5、55.8、65.1都大于y轴上限ymax=39.10,同样不在limits = c(ymin, ymax)范围内

ggplot默认只会显示落在坐标轴范围内的刻度,所以只留下了刚好在边界上的极值。以下是两种解决思路:

方案一:扩大地图范围,包含所有指定刻度值

如果需要让这些刻度对应的区域也显示在地图里,重新计算坐标轴的上下限覆盖所有breaks值,同时更新地图的截取范围:

library("ggplot2")
library("ggmap")
library("ggspatial")

register_stadiamaps("f6528bd0-7cc4-4ba1-8b78-053d0082ec72")
# 从breaks中重新计算坐标轴范围
xmin_new <- min(breaks_x); xmax_new <- max(breaks_x)
ymin_new <- min(breaks_y); ymax_new <- max(breaks_y)

pt     <- c(left = xmin_new, bottom = ymin_new, right = xmax_new, top = ymax_new)
PT_map <- get_stadiamap(pt, zoom = 8, maptype = "stamen_terrain") %>% ggmap()
breaks_x <- c(-8.9, -11.2, -13.4, -15.6, -7.8)
breaks_y <- c(37.2, 46.5, 55.8, 65.1, 39.1)

north_arrow_gg <- ggspatial::annotation_north_arrow(
    location = "tl", 
    pad_x = unit(0.1, "in"), pad_y = unit(0.1, "in"),
    height = unit(2.0, "cm"), width = unit(2.0, "cm"),
    style = ggspatial::north_arrow_nautical(
        fill = c("grey40", "white"), 
        line_col = "grey20",text_family = "ArcherPro Book"
    )
)

gg_test <- PT_map  + 
    scale_x_continuous(breaks = breaks_x, limits = c(xmin_new, xmax_new)) + 
    scale_y_continuous(breaks = breaks_y, limits = c(ymin_new, ymax_new)) +
    north_arrow_gg + 
    annotation_scale(location = "bl", width_hint = 0.5, line_width = 1) + 
    coord_sf(crs = 4326, expand = FALSE)
gg_test

注:这里把zoom从10调到8,是因为地图范围变大,需要调整缩放比例保证内容显示合适。

方案二:强制显示所有刻度(即使超出当前地图范围)

如果不需要扩大地图,只是希望轴上显示所有指定的刻度标签,可以通过设置坐标轴引导选项,强制显示所有breaks:

library("ggplot2")
library("ggmap")
library("ggspatial")

register_stadiamaps("f6528bd0-7cc4-4ba1-8b78-053d0082ec72")
xmin <- -8.93; xmax <- -7.80;  ymin <- 37.23;  ymax <- 39.10;
pt     <- c(left = xmin, bottom = ymin, right = xmax, top = ymax)
PT_map <- get_stadiamap(pt, zoom = 10, maptype = "stamen_terrain") %>% ggmap()
breaks_x <- c(-8.9, -11.2, -13.4, -15.6, -7.8)
breaks_y <- c(37.2, 46.5, 55.8, 65.1, 39.1)

north_arrow_gg <- ggspatial::annotation_north_arrow(
    location = "tl", 
    pad_x = unit(0.1, "in"), pad_y = unit(0.1, "in"),
    height = unit(2.0, "cm"), width = unit(2.0, "cm"),
    style = ggspatial::north_arrow_nautical(
        fill = c("grey40", "white"), 
        line_col = "grey20",text_family = "ArcherPro Book"
    )
)

gg_test <- PT_map  + 
    scale_x_continuous(
        breaks = breaks_x, 
        limits = c(xmin, xmax),
        guide = guide_axis(check.overlap = FALSE)  # 强制显示所有刻度,不检查重叠
    ) + 
    scale_y_continuous(
        breaks = breaks_y, 
        limits = c(ymin, ymax),
        guide = guide_axis(check.overlap = FALSE)
    ) +
    north_arrow_gg + 
    annotation_scale(location = "bl", width_hint = 0.5, line_width = 1) + 
    coord_sf(crs = 4326, expand = FALSE)
gg_test

这种方式下,超出地图范围的刻度会显示在轴上,但对应的区域不会出现在地图里,适合需要标注相关刻度但不需要展示对应区域的场景。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 22:47:06