【问题标题】:Align title of a legend in ggplot在ggplot中对齐图例的标题
【发布时间】:2017-03-11 18:34:05
【问题描述】:

我正在尝试更改 ggplot 中图例标题的对齐方式。具体来说,我想将水平图例的标题放在图例键上方而不是左侧(默认)。看来我应该可以使用theme(legend.title.align),但是该功能似乎没有任何效果。我什至尝试将它用于默认垂直对齐的右图例,但没有成功。

下面的示例使用mtcars

# Horizontal legend placed below plot
ggplot(mtcars, aes(x = hp, y = mpg, color = factor(cyl))) +
 geom_point() +
 theme(legend.position = 'bottom',
       legend.direction = 'horizontal')

# Vertical legend right of plot with attempt at right alignment
ggplot(mtcars, aes(x = hp, y = mpg, color = factor(cyl))) +
  geom_point() +
  theme(legend.position = 'right',
  legend.direction = 'vertical', 
  legend.title.align = '1')

有没有办法改变 ggplot 中 legend.title 的位置,尤其是把标题放在水平图例上方?

【问题讨论】:

    标签: r ggplot2 data-visualization


    【解决方案1】:

    guides(color = guide_legend(title.position = 'top') 解决了我的问题。我猜你只是不能在theme()中调整标题对齐方式@

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-18
      • 2020-06-01
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 2013-01-24
      相关资源
      最近更新 更多