如何在ggplot2分面绘图顶部为分组列添加A、B标签及空白分隔列
实现方案
你可以通过两种方案实现需求,都可以避免拼接时绘图区不对齐的问题:
方案1:使用ggh4x包嵌套分面(推荐)
该方案直接在单张ggplot内实现分层标题和空白分隔,不需要拼接。
步骤1:数据预处理
先给你的数据添加上层分组标签,同时插入一个虚拟空白项作为两组分面的分隔:
library(tidyverse) library(ggh4x) # 整理绘图数据 plot_data <- data.QC.1[!data.QC.1$hemiparasite %in% c("TRER", "TRVE", "CAHI"),] %>% mutate( # 定义hemiparasite的顺序:前4个A组 → 空白分隔 → 后4个B组,可根据你的实际水平数量调整索引 hemiparasite = factor(hemiparasite, levels = c( levels(hemiparasite)[1:4], "blank", levels(hemiparasite)[5:8] )), # 上层分组标签 top_group = case_when( hemiparasite %in% levels(hemiparasite)[1:4] ~ "A", hemiparasite %in% levels(hemiparasite)[6:9] ~ "B", TRUE ~ "" ) )
步骤2:修改ggplot代码
替换原有的facet_grid为facet_nested,同时设置隐藏空白分面的样式:
Carbon.p <- ggplot(plot_data, aes(x=type.full, y = Value))+ stat_summary(fun = "mean", shape = "-", size = 2.5)+ geom_jitter(aes(color = type.full), alpha = 0.5)+ # 注意你原代码这里多了一个右括号,已删除 scale_color_manual(values = c("#de2d26" ,"#9ebcda", "#8856a7")) + # 嵌套分面设置 facet_nested(host ~ top_group + hemiparasite, scales= "free", switch = "y", nest_line = element_blank(), strip = strip_nested( text_x = elem_list_text( element_text(), # 上层A/B标题样式 element_text(), # 下层hemiparasite标签样式 element_text(color = NA) # 隐藏空白项的标签 ), background_x = elem_list_rect( element_rect(), element_rect(), element_rect(fill = NA, color = NA) # 隐藏空白项的标签背景 ) ))+ theme_bw()+ theme(text = element_text(size = 10), panel.grid.minor = element_blank(), panel.grid.major = element_blank(), axis.line.x = element_blank(), axis.text.y = element_blank(), axis.ticks.y = element_blank(), axis.line.y = element_blank(), legend.title= element_blank(), legend.position = "bottom", axis.text.x = element_blank(), axis.ticks.x = element_blank(), legend.key = element_blank(), strip.background = element_rect(colour="black", fill="white"), # 隐藏空白分面的背景和边框 panel.background = element_rect(fill = c(rep("white",4), NA, rep("white",4)), color = NA), panel.border = element_rect(color = c(rep("black",4), NA, rep("black",4))))+ labs(x = NULL, y = expression("d13C"), title = NULL)
方案2:patchwork拼接对齐法
如果你不想额外安装ggh4x包,也可以用patchwork拼接时强制对齐绘图区:
library(patchwork) # 拆分两组数据 data_A <- filter(plot_data, top_group == "A") data_B <- filter(plot_data, top_group == "B") # 绘制A组图,保留y轴 pA <- ggplot(data_A, aes(x=type.full, y = Value))+ stat_summary(fun = "mean", shape = "-", size = 2.5)+ geom_jitter(aes(color = type.full), alpha = 0.5)+ scale_color_manual(values = c("#de2d26" ,"#9ebcda", "#8856a7")) + facet_grid(host ~ hemiparasite, scales= "free", switch = "y")+ theme_bw()+ theme( # 通用主题和原代码保持一致即可 )+ labs(title = "A", y = expression("d13C"))+ theme(plot.title = element_text(hjust = 0.5)) # 绘制B组图,移除y轴元素 pB <- ggplot(data_B, aes(x=type.full, y = Value))+ stat_summary(fun = "mean", shape = "-", size = 2.5)+ geom_jitter(aes(color = type.full), alpha = 0.5)+ scale_color_manual(values = c("#de2d26" ,"#9ebcda", "#8856a7")) + facet_grid(host ~ hemiparasite, scales= "free", switch = "y")+ theme_bw()+ theme( # 通用主题和原代码保持一致即可 axis.text.y = element_blank(), axis.ticks.y = element_blank() )+ labs(title = "B", y = NULL)+ theme(plot.title = element_text(hjust = 0.5)) # 拼接对齐,中间插入空白占位 pA + plot_spacer() + pB + plot_layout(widths = c(4, 0.1, 4), guides = "collect") & theme(legend.position = "bottom")
内容的提问来源于stack exchange,提问作者Skotani1
相关产品推荐
相关产品推荐

