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

ggplot2双图例场景下单个图例垂直位置调整方法咨询

问题:ggplot2多图例的相对位置控制

使用ggplot2绘制含两个图例的图表时,可通过参数调整不同图例的相对位置。本场景需要将折线对应的"Normal"图例标识,与颜色条"Some Color"的中心垂直对齐,让红色折线的图例图标高度和颜色条中心一致。

当前实现效果
期望实现效果

原问题复现代码

library(ggplot2) 
library(dplyr)

tt <- tibble(a=runif(15))
tt <- tt %>% mutate(n=row_number())

tt %>% ggplot() + 
  geom_col( aes(x=n,y=a,fill=a)) + 
  geom_line(aes(x=n,y=a,color='Normal'),size=1) +
  theme_bw() + 
  scale_color_manual(values=c('Normal'='red')) + 
  labs(title='A Title',
       x='Some Numbers',
       y='The Y-Axis',
       color = '',
       fill='Some Color') + 
  theme(plot.title   = element_text(hjust=0.5),
        axis.title.x = element_blank(),
        legend.title = element_text(size=13,face='bold'),
        legend.text  = element_text(size=13,face='bold'),
        legend.direction = "horizontal", 
        legend.position = "bottom",
        legend.key.width=unit(1.0,"cm"),
        legend.key.height=unit(.25,"cm"),
        legend.box.background = element_blank(),
        legend.justification = "center",
        legend.margin = margin(0.,0.,0.,0., unit="cm"),
        legend.background = element_blank(),
        plot.margin = margin(0.5, 0, 0, 0, "cm")) +
  guides(fill = guide_colorbar(title.position = "top",
                               title.hjust = 0.5),
         color = guide_legend(title.position = "top", 
                              label.vjust = 0.5,
                              label.position = "top",
                              order=1)
  )

解决方案

仅需对原有代码做两处修改即可实现效果:

  1. 在theme()中新增legend.box = "horizontal",明确图例的横向排列布局
  2. 在折线对应的guide_legend()中新增theme参数,单独调整该图例的上边距,实现整个图例(含图标和文字)的整体垂直位移,解决原有label.vjust参数仅移动文字、不移动图标的问题

修改后的完整代码如下:

library(ggplot2) 
library(dplyr)

tt <- tibble(a=runif(15))
tt <- tt %>% mutate(n=row_number())

tt %>% ggplot() + 
  geom_col( aes(x=n,y=a,fill=a)) + 
  geom_line(aes(x=n,y=a,color='Normal'),size=1) +
  theme_bw() + 
  scale_color_manual(values=c('Normal'='red')) + 
  labs(title='A Title',
       x='Some Numbers',
       y='The Y-Axis',
       color = '',
       fill='Some Color') + 
  theme(plot.title   = element_text(hjust=0.5),
        axis.title.x = element_blank(),
        legend.title = element_text(size=13,face='bold'),
        legend.text  = element_text(size=13,face='bold'),
        legend.direction = "horizontal", 
        legend.position = "bottom",
        legend.key.width=unit(1.0,"cm"),
        legend.key.height=unit(.25,"cm"),
        legend.box.background = element_blank(),
        legend.justification = "center",
        legend.box = "horizontal", # 新增:明确图例横向排列规则
        legend.margin = margin(0.,0.,0.,0., unit="cm"),
        legend.background = element_blank(),
        plot.margin = margin(0.5, 0, 0, 0, "cm")) +
  guides(fill = guide_colorbar(title.position = "top",
                               title.hjust = 0.5),
         color = guide_legend(title.position = "top", 
                              label.vjust = 0.5,
                              label.position = "top",
                              order=1,
                              # 新增:单独调整该图例的上边距,实现整体下移
                              theme = theme(legend.margin = margin(t = 0.3, unit = "cm")))
  )

如果对齐效果不符合预期,可调整margin(t = xxx)的数值大小,直到图标对齐即可。

内容的提问来源于stack exchange,提问作者JFD

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 13:45:04