关于r:是否可以更改ggplot2中图例项之间的间距?

Is there a way to change the spacing between legend items in ggplot2?

是否可以更改ggplot2中图例项之间的间距?我目前有

1
legend.position ="top"

会自动生成水平图例。但是,这些项目的间距非常接近,我想知道如何将它们分开得更远。


2018年7月发布的

ggplot2 v3.0.0具有修改legend.spacing.xlegend.spacing.ylegend.text的工作选项。

示例:增加图例键之间的水平间距

1
2
3
4
5
6
7
8
9
library(ggplot2)

ggplot(mtcars, aes(factor(cyl), fill = factor(cyl))) +
  geom_bar() +
  coord_flip() +
  scale_fill_brewer("Cyl", palette ="Dark2") +
  theme_minimal(base_size = 14) +
  theme(legend.position = 'top',
        legend.spacing.x = unit(1.0, 'cm'))

> </p>
<p>注意:如果只想扩大图例文本右侧的间距,请使用<wyn>stringr::str_pad()</wyn> </p>
<p>示例:将图例键标签移至底部并增加垂直间距</p>
<div class=

1
2
3
4
5
6
7
8
9
10
11
ggplot(mtcars, aes(factor(cyl), fill = factor(cyl))) +
  geom_bar() +
  coord_flip() +
  scale_fill_brewer("Cyl", palette ="Dark2") +
  theme_minimal(base_size = 14) +
  theme(legend.position = 'top',
        legend.spacing.x = unit(1.0, 'cm'),
        legend.text = element_text(margin = margin(t = 10))) +
  guides(fill = guide_legend(title ="Cyl",
                             label.position ="bottom",
                             title.position ="left", title.vjust = 1))

> </p>
<p>示例:对于<wyn>scale_fill_xxx</wyn></p>
<div class=


我认为最好的选择是在guides中使用guide_legend

1
2
3
4
5
p + guides(fill=guide_legend(
                 keywidth=0.1,
                 keyheight=0.1,
                 default.unit="inch")
      )

请注意使用default.unit,无需加载grid软件包。


一个简单的修复方法,我用于在水平图例中添加空间,只需在标签中添加空间(请参见下面的摘录):

1
2
3
4
  scale_fill_manual(values=c("red","blue","white"),
                    labels=c("Label of category 1         ",
                            "Label of category 2         ",
                            "Label of category 3"))


现在ggplot2包中已弃用opts,应改为使用函数theme

1
2
3
library(grid) # for unit()
... + theme(legend.key.height=unit(3,"line"))
... + theme(legend.key.width=unit(3,"line"))


要在图例中的条目之间增加间距,请调整主题元素legend.text的边距。

在每个图例标签的右侧添加30pt的空间(对于水平图例可能有用):

1
2
p + theme(legend.text = element_text(
    margin = margin(r = 30, unit ="pt")))

在每个图例标签的左侧添加30pt的空间(对于垂直图例可能有用):

1
2
p + theme(legend.text = element_text(
    margin = margin(l = 30, unit ="pt")))

用于ggplot2对象p。关键字是legend.textmargin

[有关编辑的注意事项:首次发布此答案时,存在一个错误。该错误现已修复]


看起来最好的方法(在2018年)是在theme对象下使用legend.key.size。 (例如,请参见此处)。

1
2
3
4
5
6
#Set-up:
    library(ggplot2)
    library(gridExtra)

    gp <- ggplot(data = mtcars, aes(mpg, cyl, colour = factor(cyl))) +
        geom_point()

如果使用theme_bw(),这真的很容易:

1
2
3
4
5
6
7
8
9
  gpbw <- gp + theme_bw()

#Change spacing size:

  g1bw <- gpbw + theme(legend.key.size = unit(0, 'lines'))
  g2bw <- gpbw + theme(legend.key.size = unit(1.5, 'lines'))
  g3bw <- gpbw + theme(legend.key.size = unit(3, 'lines'))

  grid.arrange(g1bw,g2bw,g3bw,nrow=3)

enter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  g1 <- gp + theme(legend.key.size = unit(0, 'lines'))
  g2 <- gp + theme(legend.key.size = unit(1.5, 'lines'))
  g3 <- gp + theme(legend.key.size = unit(3, 'lines'))

  grid.arrange(g1,g2,g3,nrow=3)

#Notice that the legend symbol squares get bigger (that's what legend.key.size does).

#Let's [indirectly]"control" that, too:
  gp2 <- g3
  g4 <- gp2 + theme(legend.key = element_rect(size = 1))
  g5 <- gp2 + theme(legend.key = element_rect(size = 3))
  g6 <- gp2 + theme(legend.key = element_rect(size = 10))

  grid.arrange(g4,g5,g6,nrow=3)   #see picture below, left

请注意,白色方块开始阻塞图例标题(如果我们继续增加该值,则最终变为图形本身)。

1
2
  #This shows you why:
    gt <- gp2 + theme(legend.key = element_rect(size = 10,color = 'yellow' ))

enter