ggarrange组合ggplot图时自定义行标题位置的问题
解决ggplot组合图添加自定义行标题的问题
方法1:基于ggarrange+annotate(直接在图外添加标题)
给每行第一个图添加自定义标题,通过调整注释位置和图边距,让标题显示在图外左上方:
library(ggplot2) library(ggpubr) # 创建示例图 p1 <- ggplot(mtcars, aes(mpg, disp)) + geom_point() p2 <- ggplot(mtcars, aes(mpg, hp)) + geom_point() p3 <- ggplot(mtcars, aes(mpg, drat)) + geom_point() p4 <- ggplot(mtcars, aes(mpg, wt)) + geom_point() p5 <- ggplot(mtcars, aes(mpg, qsec)) + geom_point() p6 <- ggplot(mtcars, aes(mpg, vs)) + geom_point() # 给每行第一个图添加图外标题 p1_title <- p1 + annotate("text", x = -Inf, y = Inf, label = "问卷维度1", hjust = -0.1, vjust = 1.5, size = 5, fontface = "bold") + theme(plot.margin = margin(t = 20, r = 5, b = 5, l = 5)) # 顶部留空避免标题被截断 p3_title <- p3 + annotate("text", x = -Inf, y = Inf, label = "问卷维度2", hjust = -0.1, vjust = 1.5, size = 5, fontface = "bold") + theme(plot.margin = margin(t = 20, r = 5, b = 5, l = 5)) p5_title <- p5 + annotate("text", x = -Inf, y = Inf, label = "问卷维度3", hjust = -0.1, vjust = 1.5, size = 5, fontface = "bold") + theme(plot.margin = margin(t = 20, r = 5, b = 5, l = 5)) # 组合所有图 ggarrange(p1_title, p2, p3_title, p4, p5_title, p6, ncol = 2, nrow = 3)
方法2:用cowplot单独控制标题区域
通过cowplot的plot_grid单独设置标题行,更灵活控制标题位置和高度:
library(ggplot2) library(cowplot) # 创建示例图(同方法1) p1 <- ggplot(mtcars, aes(mpg, disp)) + geom_point() p2 <- ggplot(mtcars, aes(mpg, hp)) + geom_point() p3 <- ggplot(mtcars, aes(mpg, drat)) + geom_point() p4 <- ggplot(mtcars, aes(mpg, wt)) + geom_point() p5 <- ggplot(mtcars, aes(mpg, qsec)) + geom_point() p6 <- ggplot(mtcars, aes(mpg, vs)) + geom_point() # 创建行标题文本框 title1 <- ggdraw() + draw_label("问卷维度1", fontface = "bold", size = 5) title2 <- ggdraw() + draw_label("问卷维度2", fontface = "bold", size = 5) title3 <- ggdraw() + draw_label("问卷维度3", fontface = "bold", size = 5) # 组合每行的标题和图 row1 <- plot_grid(title1, NULL, ncol = 2, rel_widths = c(1, 1)) # NULL占位对应第二列图上方 row1_full <- plot_grid(row1, plot_grid(p1, p2, ncol = 2), nrow = 2, rel_heights = c(0.1, 1)) row2 <- plot_grid(title2, NULL, ncol = 2, rel_widths = c(1, 1)) row2_full <- plot_grid(row2, plot_grid(p3, p4, ncol = 2), nrow = 2, rel_heights = c(0.1, 1)) row3 <- plot_grid(title3, NULL, ncol = 2, rel_widths = c(1, 1)) row3_full <- plot_grid(row3, plot_grid(p5, p6, ncol = 2), nrow = 2, rel_heights = c(0.1, 1)) # 组合所有行 plot_grid(row1_full, row2_full, row3_full, nrow = 3)
方法3:用patchwork简洁实现
patchwork的plot_annotation直接自定义标签,调整标签位置实现行标题效果:
library(ggplot2) library(patchwork) # 创建示例图(同方法1) p1 <- ggplot(mtcars, aes(mpg, disp)) + geom_point() p2 <- ggplot(mtcars, aes(mpg, hp)) + geom_point() p3 <- ggplot(mtcars, aes(mpg, drat)) + geom_point() p4 <- ggplot(mtcars, aes(mpg, wt)) + geom_point() p5 <- ggplot(mtcars, aes(mpg, qsec)) + geom_point() p6 <- ggplot(mtcars, aes(mpg, vs)) + geom_point() # 组合图并添加自定义行标题 wrap_plots(p1, p2, p3, p4, p5, p6, ncol = 2) + plot_annotation(tag_levels = list(c("问卷维度1", "问卷维度1", "问卷维度2", "问卷维度2", "问卷维度3", "问卷维度3"))) + theme(plot.tag = element_text(size = 5, face = "bold", hjust = 0), plot.tag.position = c(0, 1.05)) # 调整标签到每行左上方
内容的提问来源于stack exchange,提问作者tony13s
相关产品推荐
相关产品推荐

