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

ggplot2+ggh4x嵌套轴标签:隔刻度隐藏与格式调整问题

解决ggplot2结合ggh4x嵌套轴标签的两个问题

问题1:从第2个刻度开始隔一个隐藏日期(dnum)标签

自定义标签处理函数,定位需要隐藏的刻度位置,将对应日期标签置空:

custom_labels <- function(labels) {
  split_labs <- strsplit(labels, "\\.")
  dnum_labs <- sapply(split_labs, `[`, 1)
  mon_labs <- sapply(split_labs, `[`, 2)
  
  # 从第2个刻度开始,隔一个隐藏日期标签
  hide_pos <- seq(2, length(labels), 2)
  dnum_labs[hide_pos] <- ""
  
  # 用空格增加日期与月份的间距
  paste0(dnum_labs, "   ", mon_labs)
}

问题2:月份标签左对齐+增加标签间距

  • 通过guide_axis_nested(align = "left")设置嵌套标签左对齐
  • 自定义标签时用多空格分隔日期和月份,拉开间距
  • 配合theme调整x轴文本的对齐方式

完整代码

library(tidyr)
library(dplyr)
library(ggplot2)
library(ggh4x)

datz <- structure(list(dnum = c(19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 
                        27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
                        10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 
                        23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L), value = c(1, 0, 2, 
                                                                            0, 0, 2, 0, 0, 1, 0, 1, 2, 3, 70, 127, 76, 71, 45, 37, 32, 30, 
                                                                            24, 18, 15, 6, 13, 6, 8, 6, 5, 2, 3, 0, 0, 2, 3, 0, 0, 2, 0, 
                                                                            2, 1, 0), mon = c("AUG", "AUG", "AUG", "AUG", "AUG", "AUG", "AUG", 
                                                                                              "AUG", "AUG", "AUG", "AUG", "AUG", "AUG", "SEP", "SEP", "SEP", 
                                                                                              "SEP", "SEP", "SEP", "SEP", "SEP", "SEP", "SEP", "SEP", "SEP", 
                                                                                              "SEP", "SEP", "SEP", "SEP", "SEP", "SEP", "SEP", "SEP", "SEP", 
                                                                                              "SEP", "SEP", "SEP", "SEP", "SEP", "SEP", "SEP", "SEP", "SEP"
                                                                            )), class = "data.frame", row.names = c(NA, -43L))

# 自定义标签函数
custom_labels <- function(labels) {
  split_labs <- strsplit(labels, "\\.")
  dnum_labs <- sapply(split_labs, `[`, 1)
  mon_labs <- sapply(split_labs, `[`, 2)
  
  hide_pos <- seq(2, length(labels), 2)
  dnum_labs[hide_pos] <- ""
  
  paste0(dnum_labs, "   ", mon_labs)
}

ggplot(data = datz, mapping = aes(x = interaction(dnum, mon), y = value)) +
  geom_bar(stat = "identity", width = 0.25) +
  scale_x_discrete(
    guide = guide_axis_nested(
      labels = custom_labels,
      align = "left"
    )
  ) +
  theme_classic() +
  scale_y_continuous(expand = c(0, 0), breaks = seq(0, 150, 10)) +
  theme(
    panel.grid = element_blank(),
    panel.grid.major.y = element_blank(),
    axis.title.x = element_blank(),
    axis.text.x.bottom = element_text(hjust = 0)
  )

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 08:17:47