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

如何用ggh4x的facet_wrap2分组双变量仅显示单变量标签?

隐藏facet_wrap2嵌套分面中的指定层标签

要实现只保留groundSurface的分面标签、隐藏fourLoc的标签,可通过调整strip_nested()的参数精准控制每一层分面的文本显示:

修改后的代码

library(ggh4x)
library(ggplot2)

p1t <- ggplot(test, aes(x=DateTime, y=mAODscale)) +
  geom_line() +
  facet_wrap2(groundSurface ~ fourLoc, drop=FALSE, ncol=2, 
              strip=strip_nested(strips = list(
                strip_text(text = element_text()), # 保留groundSurface层的标签
                strip_text(text = element_blank()) # 隐藏fourLoc层的标签
              ), bleed=FALSE)) +
  labs(x=NULL, y='value', color=NULL) +
  theme_bw() +
  theme(axis.text.x=element_text(angle=90, vjust=0.5),
        text=element_text(size=12, family='Calibri'),
        legend.direction="horizontal",legend.position="bottom", legend.box="vertical")

plot(p1t)

关键说明

  • strip_nested()的strips参数接受列表,列表元素对应分面公式中从左到右的每一层。第一层对应groundSurface,保留正常文本样式;第二层对应fourLoc,用element_blank()完全隐藏文本。
  • 修正原代码中element_text的语法错误,添加family=参数指定字体。

数据集

test <- structure(list(loc = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), levels = c("B1", 
"HM1", "HM3"), class = "factor"), DateTime = c("01/03/2022", 
"02/03/2022", "03/03/2022", "01/03/2022", "02/03/2022", "03/03/2022", 
"01/03/2022", "02/03/2022", "03/03/2022", "01/03/2022", "02/03/2022", 
"03/03/2022", "01/03/2022", "02/03/2022", "03/03/2022", "01/03/2022", 
"02/03/2022", "03/03/2022"), calYear = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), levels = "2022", class = "factor"), 
    month = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), levels = "3", class = "factor"), 
    spi = c(0.131279514, 0.875832767, 0.105353781, 0.652808322, 
    0.348200666, 0.137788759, 0.546853561, 0.878139205, 0.649630365, 
    0.133656114, 0.960711263, 0.107314452, 0.866935108, 0.401248114, 
    0.553906897, 0.350247914, 0.59256722, 0.609365765), fourLoc = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L), levels = c("BE", "mere"), class = "factor"), mAODscale = c(0.344703325, 
    0.813991101, 0.50608775, 0.130934881, 0.502777104, 0.67505956, 
    0.440368963, 0.800293223, 0.028270253, 0.416160857, 0.033790935, 
    0.625116508, 0.341240233, 0.437566191, 0.823241792, 0.163872636, 
    0.134161724, 0.233193229), label2 = structure(c(1L, 1L, 1L, 
    1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L
    ), levels = c("B1", "HM1", "HM3"), class = "factor"), groundSurface = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 
    2L, 2L), levels = c("ground", "surface"), class = "factor"), 
    maxDate = c(1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 
    1L, 0L, 0L, 1L, 0L, 0L), MinDate = c(0L, 1L, 0L, 0L, 1L, 
    0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L), maxDate2 = c(0L, 
    0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 
    0L, 1L), noMaxDate2 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Wtstart = c(1L, 0L, 
    0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 
    0L)), row.names = c(NA, -18L), class = "data.frame")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 16:35:55