R语言:如何用cowplot(nrow=2,ncol=2)消除子图间的空白间隙
解决cowplot拼接子图时上方子图x轴空白问题
问题描述
使用R语言cowplot包以2行2列布局拼接子图时,上方两个子图(根长图、根表面积图)的x轴标签位置始终留有空白间隙,即使设置axis.text.x = element_blank()也无法消除,相关代码及输出图如下:
原始代码
# Tukeys字母标记 anova <- aov(Root_Length ~Treatment, data =JO_Data) tukey <- TukeyHSD(anova) cld <- multcompLetters4(anova, tukey) JO_Data_Root_Length <- JO_Data %>% dplyr::group_by(Treatment) %>% dplyr::summarise(mean=mean(Root_Length), sd = sd(Root_Length)) %>% arrange(desc(mean)) cld <- as.data.frame.list(cld$Treatment) JO_Data_Root_Length$cld <- cld$Letters # 根长图绘制 plot.JO_Data.Root_Length=ggplot(JO_Data_Root_Length,aes(x = Treatment, y = mean, fill=Treatment)) + geom_bar(aes(x=Treatment, y=mean), stat="identity", alpha=1) + geom_errorbar( aes(x = Treatment, ymin = mean -sd, ymax = mean +sd), width=0.4, colour="black", alpha=0.9, size=1) + geom_text(aes(label = cld, y = mean + sd), vjust = -0.5) + labs(y = expression(paste("Root Length (cm)")))+ scale_y_continuous(expand = c(0,0), limits = c(0,12000), breaks = seq(0, 12000, by = 2000)) + scale_x_discrete(limits = c("Control", "Control + UAN 50", "Control + UAN 100","Compost", "Compost + UAN 50", "LD", "LD + UAN 50", "SD", "SD + UAN 50"))+ scale_fill_manual(values = cbbPalette, breaks = c("Control", "Control + UAN 50", "Control + UAN 100","Compost", "Compost + UAN 50", "LD", "LD + UAN 50", "SD", "SD + UAN 50"))+ theme(axis.line = element_line(colour = "black"), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank(), panel.background = element_blank(), axis.text.x = element_blank(), axis.text.y = element_text(size = 12), axis.title.y = element_text(size = 12))+ theme(legend.justification = c("right", "top"))+ theme(legend.title = element_text(color = "black", size = 16, face = "bold"))+ theme(legend.title.align = 0.5) + theme(legend.background = element_rect(linetype = "solid", colour = "black", size = 1))+ theme(legend.margin = margin(0.1,0.2,0.2,0.2, unit ="cm"))+ theme(legend.text = element_text(size=14, colour="black"))+ theme(axis.title.x=element_blank()) plot.JO_Data.Root_Length ################### 其余三个图代码与根长图结构一致,此处省略 ######## # 子图拼接 library(gridExtra) library(cowplot) JO_plot2 <- plot_grid(plot.JO_Data.Root_Length + theme(legend.position="none"), plot.JO_Data.Root_SA + theme(legend.position="none"), plot.JO_Data.Root_diameter + theme(legend.position="none"), plot.JO_Data.Root_Vol + theme(legend.position="none"), align = "h", axis = "b", hjust = 0.1, vjust = 0.1, nrow = 2, ncol =2) legend <- get_legend(plot.JO_Data.Root_Length) Root_plot2 <- plot_grid(JO_plot2, legend, rel_widths = c(1.75, 0.5)) Root_plot2
输出图
解决方案
核心问题分析
axis.text.x = element_blank()仅移除了x轴文本,但x轴刻度、以及plot_grid中axis = "b"参数强制所有子图底部对齐的设置,会继续保留空白空间。
具体修改步骤
移除
plot_grid中的axis = "b"参数
该参数会强制所有子图的底部(x轴)对齐,导致上方子图必须预留与下方子图x轴一致的空白。删除后,上方子图可自由压缩底部空间。修改上方子图的theme配置
在根长图和根表面积图的theme中添加以下设置,彻底清除x轴相关空白:axis.ticks.x = element_blank():移除x轴刻度(默认会占据空间)plot.margin = margin(t=10, r=10, b=0, l=10, unit="pt"):将底部边距设为0,消除最后残留的空白
(可选)调整对齐方式
若需要保证子图左侧y轴对齐,可将align参数设置为"v"(垂直对齐)。
修改后的关键代码片段
修改后的根长图theme部分
theme(axis.line = element_line(colour = "black"), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank(), panel.background = element_blank(), axis.text.x = element_blank(), axis.ticks.x = element_blank(), # 新增:移除x轴刻度 axis.text.y = element_text(size = 12), axis.title.y = element_text(size = 12), plot.margin = margin(t=10, r=10, b=0, l=10, unit="pt") # 新增:缩小底部边距 ) + # 其余theme设置保持不变
修改后的plot_grid部分
JO_plot2 <- plot_grid(plot.JO_Data.Root_Length + theme(legend.position="none"), plot.JO_Data.Root_SA + theme(legend.position="none"), plot.JO_Data.Root_diameter + theme(legend.position="none"), plot.JO_Data.Root_Vol + theme(legend.position="none"), align = "v", # 仅垂直对齐左侧y轴 hjust = 0.1, vjust = 0.1, nrow = 2, ncol =2)
内容的提问来源于stack exchange,提问作者James
相关产品推荐
相关产品推荐

