ggarrange输出异常求助:图例重叠模糊、Y轴标签致对齐问题
问题描述
使用ggarrange将6幅图排列为2列3行布局,设置了共用图例与共用Y轴标题。此前代码可正常生成预期输出,但重新绘图后出现两个问题:
- 图例因两组元素重叠变得模糊不清
- 原共用Y轴标签的实现方案(仅保留第三幅图的Y轴标签)导致左列Y轴对齐混乱
解决方案
1. 修复图例重叠问题
问题根源:每幅图都自带图例,ggarrange的common.legend=TRUE虽会提取共用图例,但原图表的图例并未被移除,导致底层图例和新添加的共用图例重叠。
解决方式:在所有子图中统一移除图例,仅让ggarrange生成共用图例。可通过自定义主题设置legend.position="none"实现。
2. 修复Y轴对齐与共用Y轴标签问题
原方案仅保留第三幅图的Y轴标签,导致左列上下图的Y轴宽度不一致,出现对齐偏移。正确做法:
- 移除所有子图的Y轴标签
- 使用
annotate_figure的left参数统一添加左侧共用Y轴标签,精准控制位置与样式
修改后的完整代码
require(tidyverse) require(grid) require(ggpubr) require(extrafont) loadfonts(device = "win") # 构造数据 list(type = rep(c("(1)", "(2)", "(3)", "(4)","(5)", "(6)"), each = 16), group = rep(rep(c("First Group", "Second Group"), each = 8), 6), x = rep(c(-7, -5, -3, -1, 1, 3, 5, 7), times = 12), scale_x = c(0.087, 0.085, 0.083, 0.081, 0.079, 0.077, 0.075, 0.073, 0.087, 0.085, 0.083, 0.081, 0.079, 0.077, 0.075, 0.073, -7, -5, -3, -1, 1, 3, 5, 7, -7, -5, -3, -1, 1, 3, 5, 7, 2.2, 2, 1.8, 1.6, 1.4, 1.2, 1, 0.8, 2.2, 2, 1.8, 1.6, 1.4, 1.2, 1, 0.8, 77, 75, 73, 71, 69, 67, 65, 63, 77, 75, 73, 71, 69, 67, 65, 63, -7, -5, -3, -1, 1, 3, 5, 7, -7, -5, -3, -1, 1, 3, 5, 7, 8.49, 8.51, 8.53, 8.55, 8.57, 8.59, 8.61, 8.63, 8.49, 8.51, 8.53, 8.55, 8.57, 8.59, 8.61, 8.63), y = rep(c(rep(c(-0.7), times = 4), rep(c(0.7), times = 4), rep(c(-0.77), times = 4), rep(c(0.77), times = 4)), times = 6)) %>% as_tibble() -> df # 绘图函数 function_graph <- function(data){ data %>% ggplot(aes(x = x, y = y, col = group)) + geom_line(aes(group = interaction(group))) + geom_point(position = position_dodge(width = .2)) + geom_vline(xintercept = 0, linetype = 2) + theme_minimal() + scale_color_manual(values = c("red", "black")) + scale_y_continuous(breaks = seq(-1, 1, length.out = 6)) + coord_cartesian(ylim = c(-1, 1), xlim = c(7, -7)) + geom_text(aes(label = "left"), y = .95, x = 3.5, size = 3, col = "gray36", family = "Palatino Linotype") + geom_text(aes(label = "right"), y = .95, x = -3.5, size = 3, col = "gray36", family = "Palatino Linotype") } # 生成6幅子图 df %>% filter(grepl("(1)", type)) %>% pull(scale_x) %>% distinct() %>% arrange() -> x_labels p_1 <- df %>% filter(grepl("(1)", type)) %>% function_graph() + labs(x = "Unit for graph (1)") + scale_x_continuous(breaks = rev(seq(-7, 7, length.out = 8)), labels = x_labels) df %>% filter(grepl("(2)", type)) %>% pull(scale_x) %>% distinct() %>% arrange() -> x_labels p_2 <- df %>% filter(grepl("(2)", type)) %>% function_graph() + labs(x = "Unit for graph (2)") + scale_x_continuous(breaks = rev(seq(-7, 7, length.out = 8)), labels = x_labels) df %>% filter(grepl("(3)", type)) %>% pull(scale_x) %>% distinct() %>% arrange() -> x_labels p_3 <- df %>% filter(grepl("(3)", type)) %>% function_graph() + labs(x = "Unit for graph (3)") + scale_x_continuous(breaks = rev(seq(-7, 7, length.out = 8)), labels = x_labels) df %>% filter(grepl("(4)", type)) %>% pull(scale_x) %>% distinct() %>% arrange() -> x_labels p_4 <- df %>% filter(grepl("(4)", type)) %>% function_graph() + labs(x = "Unit for graph (4)") + scale_x_continuous(breaks = rev(seq(-7, 7, length.out = 8)), labels = x_labels) df %>% filter(grepl("(5)", type)) %>% pull(scale_x) %>% distinct() %>% arrange() -> x_labels p_5 <- df %>% filter(grepl("(5)", type)) %>% function_graph() + labs(x = "Unit for graph (5)") + scale_x_continuous(breaks = rev(seq(-7, 7, length.out = 8)), labels = x_labels) df %>% filter(grepl("(6)", type)) %>% pull(scale_x) %>% distinct() %>% arrange() -> x_labels p_6 <- df %>% filter(grepl("(6)", type)) %>% function_graph() + labs(x = "Unit for graph (6)") + scale_x_continuous(breaks = rev(seq(-7, 7, length.out = 8)), labels = x_labels) # 自定义主题:移除子图图例 theme_own <- function(){ theme_minimal() %+replace% theme( plot.caption = element_text(color="black", size = 11, hjust = 1), axis.title = element_text(size = 9), axis.text = element_text(size = 7), plot.title=element_text(hjust=0.5, face="bold", size=13, vjust = 2), text = element_text(family = "Palatino Linotype"), legend.position="none", # 关键:子图不显示图例 legend.title = element_blank() ) } # 组合图表并添加标题和共用Y轴 ggarrange(p_1 + theme_own(), p_2 + theme_own(), p_3 + theme_own(), p_4 + theme_own(), p_5 + theme_own(), p_6 + theme_own(), ncol=2, nrow=3, common.legend = TRUE, legend="bottom") %>% annotate_figure( top = text_grob("A nice title", color = "black", face = "bold", size = 14, family = "Palatino Linotype"), left = text_grob("Common y-axis label", rot = 90, size = 9, family = "Palatino Linotype") # 添加共用Y轴标签 )
内容的提问来源于stack exchange,提问作者Luise
相关产品推荐
相关产品推荐

