使用patchwork组合ggplot图时y轴标签对齐问题求助
如何对齐带facet的ggplot与其他图的y轴标签?
我用ggplot创建了三个独立绘图,随后使用patchwork将它们垂直组合在一起。第三个绘图借助facet_grid添加了显示年份的次级x轴,但这导致它的y轴标签与绘图紧密相连,没有像前两个图那样留有间隔。尝试调整margin、vjust/hjust都未能解决问题,请问有什么方法可以对齐y轴标签?
我的代码:
#First graph presence <- ggplot(df_long, aes(x = date)) + geom_bar(aes(y = total_presence, fill = "Presence"), stat = "identity", width = 1, position = "stack") + geom_bar(aes(y = total_song, fill = "Song"), stat = "identity", width = 1, position = "stack") + geom_bar(data = filter(df_long, is.na(total_presence) & is.na(total_song)), aes(x=date, y =24), fill = "azure3", stat="identity", width = 1, position = "stack") + scale_fill_manual(values = c("Presence" = "olivedrab", "Song" = "olivedrab3"), name = "Detections") + scale_y_continuous(name = "Presence", breaks = seq(0,24,by=2), limits = c(0,24),expand=c(0,0))+ scale_x_date(date_labels = "%b", date_breaks="1 month", expand= c(0,0))+ labs(x=NULL, y = "Total Detections", title = paste(location)) + theme_minimal() + theme(axis.title.y = element_text(color = "black"), axis.text.y = element_text(color = "black"), axis.ticks.y=element_line(color="black"), panel.grid = element_blank(), legend.position = "right", plot.title = element_text(hjust = 0.5)) #Second graph ice_temp <- ggplot(df_long, aes(x=date)) + geom_bar(aes(y=Proportion,fill=Ice_Type), stat = "identity", position = "stack", width = 1) + geom_line(aes(y = (sst_mean/4)+0.5), color = "red", size = 0.4) + labs(x=NULL, y="Sea Ice Proportion", title = NULL)+ scale_fill_manual(values = c( "VeryOpenDriftIce" = "#7FDBFF", "OpenDriftIce" = "#0074D9", "CloseDriftIce" = "#00457e", "VeryCloseDriftIce" = "#001f3f", "FastIce" = "red" )) + theme_minimal() + scale_y_continuous(limits=c(0, NA), expand=c(0,0), sec.axis = sec_axis(~ (.-0.5)*4, name = "Sea Surface Temperature (°C)", breaks = seq(-2,2,by=1)))+ scale_x_date(date_labels = "%b", date_breaks="1 month", expand= c(0,0))+ theme(panel.grid = element_blank(), legend.position = "right", axis.ticks = element_line(), axis.title.y.right = element_text(color = "red"), axis.text.y.right = element_text(color = "red"), axis.ticks.y.right=element_line(color="red")) #third graph zooc_chl <- ggplot(df_long, aes(x=date)) + geom_line(aes(y=zooc_mean),color="#D55E00", size = 0.4) + geom_line(aes(y=chl_mean),color="#0072B2",size=0.4)+ labs(x=NULL, y="<span style = 'color:#D55E00;'>Zooplankton [g/m<sup>2</sup>]</span> <span style = 'color:#0072B2;'>Chlorophyll a [mg/m<sup>3</sup>]</span>", title = NULL)+ scale_y_continuous(breaks = seq(0,3,by=0.5), limits = c(0,3),expand=c(0,0))+ scale_x_date(date_labels = "%b", date_breaks="1 month", expand= c(0,0))+ facet_grid(~ year(date), space= "free", scales="free",switch="x") + theme_minimal() + theme(axis.title.y=element_markdown(), strip.placement = "outside", axis.ticks = element_line(), strip.background = element_rect(fill=NA,colour="grey50"), panel.spacing=unit(0,"cm")) #combine graphs combined_plot <- presence / ice_temp / zooc_chl
问题效果:

解决方案
问题根源是facet_grid的switch="x"将年份strip移到x轴外侧,改变了绘图区域的边距计算逻辑,压缩了y轴标签的可用空间。以下三种方法都能解决:
方法1:统一设置所有图的左边距
给三个图的theme添加相同的左边距,确保y轴标签有足够的显示空间。数值可以根据标签长度调整:
修改第三个图的theme:
theme_minimal() + theme(axis.title.y=element_markdown(), strip.placement = "outside", axis.ticks = element_line(), strip.background = element_rect(fill=NA,colour="grey50"), panel.spacing=unit(0,"cm"), plot.margin = margin(t=5, r=5, b=5, l=20, unit="pt")) # 统一左边距
前两个图也需要添加同样的plot.margin参数,确保三个图的左侧边距完全一致。
方法2:用patchwork全局统一边距
无需逐个修改图,在拼接时通过plot_layout配合全局theme设置统一边距:
combined_plot <- presence / ice_temp / zooc_chl + plot_layout(guides = "collect") + # 可选:合并图例,优化布局 theme(plot.margin = margin(t=5, r=5, b=5, l=20, unit="pt"))
方法3:单独调整facet图的y轴标题边距
针对第三个图,直接给y轴标题添加右侧边距,让它和绘图区域拉开距离,匹配前两个图的标签位置:
theme_minimal() + theme(axis.title.y=element_markdown(margin = margin(r=10, unit="pt")), # 给y轴标题加右侧间隔 strip.placement = "outside", axis.ticks = element_line(), strip.background = element_rect(fill=NA,colour="grey50"), panel.spacing=unit(0,"cm"))
调整后重新拼接,三个图的y轴标签即可对齐。
内容的提问来源于stack exchange,提问作者Lucat
相关产品推荐
相关产品推荐

