如何在R中为两个拼接的分面图添加右侧Y轴标签?
解决方案:为patchwork拼接的分面图添加右侧Y轴标签
方法1:为每个子图添加独立的右侧Y轴标签
这种方法通过scale_y_continuous()的sec.axis参数配合主题设置,让每个子图的右侧显示对应Y轴标签,适合上下图需要不同标签的场景。
library(ggplot2) library(patchwork) # 第一个数据集与绘图 data_sample <- data.frame( group = rep(c("A", "B"), each = 20), n_classes = rep(c("xyz", "zyx"), each = 10), indicator = rep(c("Indicator 1", "Indicator 2"), each = 20), ICC = runif(40, 0, 1), extrapolation_rate = runif(40, 0, 1) ) basic_facet_plot1 <- ggplot(data_sample, aes(x = ICC, y = extrapolation_rate, color = group)) + geom_point() + facet_grid(indicator ~ n_classes) + theme_minimal() + labs(title = "Sample Facet Plot", y = "Something", color = "Group") + theme(axis.title.x = element_blank()) + # 添加右侧Y轴并配置标签 scale_y_continuous(sec.axis = dup_axis(name = "Something")) + theme( axis.title.y.right = element_text(angle = 90, margin = margin(l = 15)), axis.ticks.y.right = element_blank(), # 可选:隐藏右侧刻度线 axis.text.y.right = element_blank() # 可选:隐藏右侧刻度文本 ) # 第二个数据集与绘图 data_sample <- data.frame( group = rep(c("A", "B"), each = 20), n_classes = rep(c("abc", "cba"), each = 10), indicator = rep(c("Indicator 3", "Indicator 4"), each = 20), ICC = runif(40, 0, 1), extrapolation_rate = runif(40, 0, 1) ) basic_facet_plot2 <- ggplot(data_sample, aes(x = ICC, y = extrapolation_rate, color = group)) + geom_point() + facet_grid(indicator ~ n_classes) + theme_minimal() + labs(y = "Something else", color = "Group", x = "ICC") + # 添加右侧Y轴并配置标签 scale_y_continuous(sec.axis = dup_axis(name = "Something else")) + theme( axis.title.y.right = element_text(angle = 90, margin = margin(l = 15)), axis.ticks.y.right = element_blank(), axis.text.y.right = element_blank() ) # 调整边距并拼接 basic_facet_plot1 <- basic_facet_plot1 + theme(plot.margin = margin(t = 1, r = 1, b = -2, l = 1, unit = "cm")) basic_facet_plot2 <- basic_facet_plot2 + theme(plot.margin = margin(t = 0, r = 1, b = 0, l = 1, unit = "cm")) combined_plot <- basic_facet_plot1 / basic_facet_plot2 final_plot <- combined_plot + plot_layout(guides = 'collect') & theme(legend.position = 'bottom') print(final_plot)
方法2:添加全局右侧Y轴标签
如果需要一个统一的右侧标签,可借助grid包创建文本对象,配合patchwork布局将标签添加到拼接图右侧。
library(ggplot2) library(patchwork) library(grid) # 原始子图代码(保持原有逻辑) data_sample <- data.frame( group = rep(c("A", "B"), each = 20), n_classes = rep(c("xyz", "zyx"), each = 10), indicator = rep(c("Indicator 1", "Indicator 2"), each = 20), ICC = runif(40, 0, 1), extrapolation_rate = runif(40, 0, 1) ) basic_facet_plot1 <- ggplot(data_sample, aes(x = ICC, y = extrapolation_rate, color = group)) + geom_point() + facet_grid(indicator ~ n_classes) + theme_minimal() + labs(title = "Sample Facet Plot", y = "Something", color = "Group") + theme(axis.title.x = element_blank()) data_sample <- data.frame( group = rep(c("A", "B"), each = 20), n_classes = rep(c("abc", "cba"), each = 10), indicator = rep(c("Indicator 3", "Indicator 4"), each = 20), ICC = runif(40, 0, 1), extrapolation_rate = runif(40, 0, 1) ) basic_facet_plot2 <- ggplot(data_sample, aes(x = ICC, y = extrapolation_rate, color = group)) + geom_point() + facet_grid(indicator ~ n_classes) + theme_minimal() + labs(y = "Something else", color = "Group", x = "ICC") # 调整边距(缩小右侧边距避免空白) basic_facet_plot1 <- basic_facet_plot1 + theme(plot.margin = margin(t = 1, r = 0, b = -2, l = 1, unit = "cm")) basic_facet_plot2 <- basic_facet_plot2 + theme(plot.margin = margin(t = 0, r = 0, b = 0, l = 1, unit = "cm")) # 创建全局右侧Y轴标签 right_y_label <- textGrob("全局Y轴标签", rot = 90, gp = gpar(fontsize = 12, fontface = "bold")) # 创建空白占位图放置标签 spacer <- plot_spacer() + theme_void() + annotation_custom(right_y_label, xmin=0, xmax=1, ymin=0, ymax=1) # 拼接布局:左侧为上下子图,右侧为标签区域 combined_plot <- (basic_facet_plot1 / basic_facet_plot2) | spacer + plot_layout(widths = c(25, 1)) # 调整宽度比例,控制标签区域宽度 # 整理图例 final_plot <- combined_plot + plot_layout(guides = 'collect') & theme(legend.position = 'bottom') print(final_plot)
内容的提问来源于stack exchange,提问作者Linus
相关产品推荐
相关产品推荐

