You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ggarrange中调整公共图例边距且不增大绘图宽度?

调整ggarrange公共图例边距的解决方案

问题说明

需要调整ggarrange生成的公共图例边距,但不想增大整体绘图宽度。

可行方案

方案1:直接在ggplot主题中细化图例参数

通过调整theme()中的图例相关参数,缩小图例内部/外部的空白,同时控制图例元素大小,让图例更紧凑:

  • legend.key.spacing.y:缩小图例项之间的垂直间距
  • legend.key.size:缩小图例符号的尺寸
  • legend.margin:去掉图例框内部的边距
  • legend.box.margin:调整图例框外部的边距

修改后的绘图代码:

data1_df = data.frame(x = 1:4, y = 5e-6, type = 'type1')
data2_df = data.frame(x = 1:4, y = 6e-6, type = 'type2')
data3_df = data.frame(x = 1:4, y = 8e-6, type = 'type3')
data4_df = data.frame(x = 1:4, y = 9e-6, type = 'type4')

dataDF = rbind(data1_df, data2_df, data3_df, data4_df)

for(i in 1:4){
  plotG = ggplot(dataDF,aes(x = x, y = y, color = type, shape = type))+
    geom_line(aes(group = type), show.legend = F)+
    geom_point(aes(group = type), size = 3)+
    scale_color_manual(breaks = c("type1","type2","type3","type4"), 
                       values = c("darkorange3","lightblue3","skyblue1","dodgerblue3"))+
    scale_shape_manual(breaks = c("type1","type2","type3","type4"), 
                       values = c(3,16,17, 18))+
    xlab('test')+ylab('Slope (= 1/4 * Va)')+
    theme_bw()+ggtitle('Time 3')+
    guides(color = guide_legend(byrow = TRUE), shape = guide_legend(byrow = TRUE))+
    theme(
      # 缩小图例项垂直间距
      legend.key.spacing.y = unit(0.2, "cm"),
      # 缩小图例符号尺寸
      legend.key.size = unit(0.8, "line"),
      # 去掉图例内部边距
      legend.margin = margin(t=0, r=0, b=0, l=0, unit="cm"),
      # 调整图例外部边距(按需微调)
      legend.box.margin = margin(t=0, r=5, b=0, l=5, unit="pt")
    )
  assign(paste0('plot',i), plotG)
}

final_plot = ggarrange(plot1, plot2, plot3, plot4, 
                       labels = c("A","B","C","D"), 
                       ncol = 2, nrow = 2,
                       common.legend = TRUE,
                       legend = 'right',
                       # 直接指定图例宽度,避免拉伸绘图
                       legend.width = unit(2, "cm"))

方案2:单独提取并修改图例后传入ggarrange

先从单个绘图中提取图例对象,调整样式后再通过legend.grob参数传入ggarrange,灵活性更高:

# 先创建基础绘图(保留原样式,可调整theme)
base_plot = ggplot(dataDF,aes(x = x, y = y, color = type, shape = type))+
  geom_line(aes(group = type), show.legend = F)+
  geom_point(aes(group = type), size = 3)+
  scale_color_manual(breaks = c("type1","type2","type3","type4"), 
                     values = c("darkorange3","lightblue3","skyblue1","dodgerblue3"))+
  scale_shape_manual(breaks = c("type1","type2","type3","type4"), 
                     values = c(3,16,17, 18))+
  xlab('test')+ylab('Slope (= 1/4 * Va)')+
  theme_bw()+ggtitle('Time 3')+
  guides(color = guide_legend(byrow = TRUE), shape = guide_legend(byrow = TRUE))

# 生成4个绘图
for(i in 1:4){
  assign(paste0('plot',i), base_plot)
}

# 提取图例并调整样式
custom_legend = get_legend(base_plot + 
                             theme(legend.position = "right") +
                             theme(
                               legend.key.spacing.y = unit(0.2, "cm"),
                               legend.key.size = unit(0.8, "line"),
                               legend.margin = margin(0,0,0,0, "cm")
                             ))

# 组合绘图与自定义图例
final_plot = ggarrange(plot1, plot2, plot3, plot4, 
                       labels = c("A","B","C","D"), 
                       ncol = 2, nrow = 2,
                       common.legend = TRUE,
                       legend.grob = custom_legend)

关键说明

  • 两种方法都无需增大整体绘图宽度,通过压缩图例内部空白、缩小元素尺寸来控制图例占用空间
  • 可根据实际需求微调unit()中的数值,达到理想的紧凑效果

内容的提问来源于stack exchange,提问作者CaffèSospeso

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 23:29:59