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

如何在ggplot2使用geom_sf_text时移除x、y轴标签?

解决ggplot2中geom_sf_text导致轴标签无法隐藏的问题

问题描述

使用ggplot2绘制西班牙空间地图时,添加geom_sf_text()标注区域名称后,原本通过theme()设置隐藏的x、y轴标签重新出现;移除geom_sf_text()后,轴标签能正常隐藏。

解决方案

geom_sf_text()会默认触发轴标题的显示,仅设置axis.text和axis.ticks为element_blank()不足以隐藏轴标签,需要在theme()中明确将轴标题设置为隐藏。

修改后的完整代码

map <- read_sf("https://raw.githubusercontent.com/R-CoderDotCom/data/main/shapefile_spain/spain.geojson")

ggplot(map) +
  geom_sf(color = "white", aes(fill = unemp_rate)) +
  geom_sf_text(aes(label = name)) +
  theme(
    legend.position = "none",
    panel.grid = element_blank(),
    axis.text = element_blank(),
    axis.ticks = element_blank(),
    axis.title.x = element_blank(),  # 隐藏x轴标题
    axis.title.y = element_blank()   # 隐藏y轴标题
  )

简化写法

也可以用axis.title = element_blank()一次性隐藏所有轴标题,代码更简洁:

ggplot(map) +
  geom_sf(color = "white", aes(fill = unemp_rate)) +
  geom_sf_text(aes(label = name)) +
  theme(
    legend.position = "none",
    panel.grid = element_blank(),
    axis.text = element_blank(),
    axis.ticks = element_blank(),
    axis.title = element_blank()  # 批量隐藏轴标题
  )

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 04:05:01