[英]Add a line break after the title of an horizontal legend title
When theme(legend.position = "top")
, the legend is horizontal and can be a bit too wide.当
theme(legend.position = "top")
时,图例是水平的,可能有点太宽了。
Here is an example:这是一个例子:
library(ggplot2)
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) +
geom_point() +
xlim(5,6) +
coord_fixed() +
theme(legend.position = "top")
Created on 2021-01-14 by the reprex package (v0.3.0)由reprex package (v0.3.0) 创建于 2021-01-14
In order to reduce the width of the legend, I'd like to add a line break after the "Species" title.为了减少图例的宽度,我想在“物种”标题后添加一个换行符。
I'm aware of guides(guide_legend(ncol=2,nrow=2,byrow=TRUE))
, it is very nice but it yeilds a different output.我知道
guides(guide_legend(ncol=2,nrow=2,byrow=TRUE))
,它非常好,但它产生了不同的 output。
We can use the title.position
argument inguide_legend
in scale_colour_discrete
:我们可以在
guide_legend
scale_colour_discrete
使用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"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.