首页 > 解决方案 > 左对齐多行ggplot2图例

问题描述

我有一个具有不同宽度的两部分图例:

library(dplyr)
library(ggplot2)
mtcars %>% 
  mutate(qsec = factor(qsec)) %>% 
  ggplot(aes(mpg, cyl, fill = qsec, linetype = 'short legend')) +
  geom_col() +
  guides(fill = guide_legend(title.position = 'top', label.position = 'bottom'),
         linetype = guide_legend(title.position = 'top', 
                                  label.position = 'bottom')) +
  theme(legend.position = 'bottom',
        legend.box = 'vertical')

是否可以左对齐linetype图例以匹配填充图例?

reprex 包(v0.3.0)于 2021-01-21 创建

标签: rggplot2

解决方案


也许legend.box.just = "left"参数是您正在寻找的?

mtcars %>% 
  mutate(qsec = factor(qsec)) %>% 
  ggplot(aes(mpg, cyl, fill = qsec, linetype = 'short legend')) +
  geom_col() +
  guides(fill = guide_legend(title.position = 'top', label.position = 'bottom'),
         linetype = guide_legend(title.position = 'top', 
                                 label.position = 'bottom')) +
  theme(legend.position = 'bottom', 
        legend.box.just = "left",
        legend.box = "vertical" )

在此处输入图像描述


推荐阅读