在水平图例标题后添加换行符

Add a line break after the title of an horizontal legend title

theme(legend.position = "top") 时,图例是水平的,可能有点太宽了。

这是一个例子:


library(ggplot2)
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) + 
  geom_point() + 
  xlim(5,6) + 
  coord_fixed() +
  theme(legend.position = "top")

reprex package (v0.3.0)

于 2021 年 1 月 14 日创建

为了减少图例的宽度,我想在“物种”标题后添加一个换行符。

我知道 guides(guide_legend(ncol=2,nrow=2,byrow=TRUE)),它非常好,但它产生不同的输出。

我们可以在scale_colour_discrete中使用guide_legend中的title.position参数:

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) + 
  geom_point() + 
  xlim(5,6) + 
  coord_fixed() +
  theme(legend.position = "top")+
  scale_colour_discrete(guide = guide_legend(title.position = "top"))