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

如何用ggh4x::facet_nested()仅调整特定嵌套分面的面板间距?

如何在ggh4x嵌套分面中仅增大特定分组间的面板间距

问题描述

我希望仅增大两个特定分面(而非图中其他嵌套分面)之间的面板间距,能否通过ggh4x实现?或是有其他可行方法?以下示例中,目标是在group分面(Maternal与Paternal)间增加间距,而嵌套的clusters分面间无额外间距。

原测试代码:

library(ggh4x)
library(ggplot2)
library(viridis)

# 创建模拟数据集
Cluster <- rep(paste("Cluster", 1:6), each = 15)
group <- ifelse(Cluster == "Cluster 1", "Paternal", "Maternal")
Cytokine <- rep(c("gmcsf_a_YN", "il2_a_YN", "il5_a_YN", "il12_p70_a_YN"), times = c(3, 3, 4, 3), length.out = 90)
Analyte <- rep(c("GM-CSF", "IL-2", "IL-5", "IL-12(p70)"), times = c(3, 3, 4, 3), length.out = 90)
component <- factor(rep(c("V1", "V2", "V3", "V7"), times = 90)[1:90])
weight <- runif(90, 0, 0.5)
combined_data <- data.frame(Cluster, Cytokine, Analyte, component, weight, group)


# ggh4x自定义分面标签
custom_strips <- strip_nested(
  bleed = FALSE,
  background_x = elem_list_rect(colour =  c("white","white","white","darkgrey","darkgrey","darkgrey","darkgrey","darkgrey"),
                                fill = c("white","white", "white","#440154FF","#3B528BFF","#21908CFF","#5DC863FF","#FDE725FF")),
  text_x = elem_list_text(colour = c("black", "black","white","white","white","white","white","black"),
                          face = c("bold", "bold")))

# 绘图
 g <- ggplot(combined_data, aes(x = Analyte, y = component)) +
  geom_point(aes(size = abs(weight), color = weight), stroke = 0.5, color = "grey30") + 
  geom_point(aes(size = abs(weight), color = weight)) +  
  scale_color_gradient2(low = "blue", high = "red", mid = "white", midpoint = 0, limit = c(-1, 1)) +
  scale_size_continuous(range = c(0, 10)) +
  ggh4x::facet_nested(~group + Cluster, scales = "free_x", space = "free_x", nest_line = element_line(linetype = 1, size = 0.5 ,colour = "darkgrey"), strip = custom_strips) +  
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1), 
        panel.spacing.x = unit(0, "lines"), 
        panel.border = element_rect(colour = "darkgrey", fill = NA, size = 0.5)) +
  labs(color = "Weight", size = "Magnitude of Weight")
 
 g 

解决方案:通过ggh4x自定义面板间距

可以直接通过theme()中的panel.spacing.x参数实现,核心是给不同的面板间隙设置差异化宽度:

修改后的完整代码

library(ggh4x)
library(ggplot2)
library(viridis)

# 创建模拟数据集
Cluster <- rep(paste("Cluster", 1:6), each = 15)
group <- ifelse(Cluster == "Cluster 1", "Paternal", "Maternal")
Cytokine <- rep(c("gmcsf_a_YN", "il2_a_YN", "il5_a_YN", "il12_p70_a_YN"), times = c(3, 3, 4, 3), length.out = 90)
Analyte <- rep(c("GM-CSF", "IL-2", "IL-5", "IL-12(p70)"), times = c(3, 3, 4, 3), length.out = 90)
component <- factor(rep(c("V1", "V2", "V3", "V7"), times = 90)[1:90])
weight <- runif(90, 0, 0.5)
combined_data <- data.frame(Cluster, Cytokine, Analyte, component, weight, group)


# ggh4x自定义分面标签
custom_strips <- strip_nested(
  bleed = FALSE,
  background_x = elem_list_rect(colour =  c("white","white","white","darkgrey","darkgrey","darkgrey","darkgrey","darkgrey"),
                                fill = c("white","white", "white","#440154FF","#3B528BFF","#21908CFF","#5DC863FF","#FDE725FF")),
  text_x = elem_list_text(colour = c("black", "black","white","white","white","white","white","black"),
                          face = c("bold", "bold")))

# 绘图
 g <- ggplot(combined_data, aes(x = Analyte, y = component)) +
  geom_point(aes(size = abs(weight), color = weight), stroke = 0.5, color = "grey30") + 
  geom_point(aes(size = abs(weight), color = weight)) +  
  scale_color_gradient2(low = "blue", high = "red", mid = "white", midpoint = 0, limit = c(-1, 1)) +
  scale_size_continuous(range = c(0, 10)) +
  ggh4x::facet_nested(~group + Cluster, scales = "free_x", space = "free_x", nest_line = element_line(linetype = 1, size = 0.5 ,colour = "darkgrey"), strip = custom_strips) +  
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1), 
        # 关键修改:为每个面板间隙设置不同宽度
        panel.spacing.x = unit(c(2, 0, 0, 0, 0), "lines"), 
        panel.border = element_rect(colour = "darkgrey", fill = NA, size = 0.5)) +
  labs(color = "Weight", size = "Magnitude of Weight")
 
 g 

关键说明

  • panel.spacing.x接受一个长度等于分面间隙数量的单位向量:示例中有6个Cluster分面,对应5个间隙。
  • 向量中第一个元素对应Paternal(Cluster1)和Maternal(Cluster2)之间的间隙,我们将其设为2 lines,其余间隙保持0 lines,实现仅增大目标分组间的间距。
  • 如果后续分面顺序调整,需要对应修改向量中数值的位置,确保目标间隙匹配正确的位置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 05:50:56