ggplot
中的图例可以作为水平图例移动到图形的底部,方法是将以下参数添加到 主题
功能:
legend.position="bottom"
将图例移动到图表下方legend.direction="horizontal"
将图例定向为水平。
然而,并不是真的......
legend.direction="horizontal"
似乎只是减少了图例中的行数和每行 中图例对象的数量。
这可以使用 guides(color=guide_legend(nrow=x)
dat <- data.frame(plot = rep(letters,2), val = rep(1:length(letters),2))
library(ggplot2)
ggplot(dat, aes(x = val, y = val, color = plot)) +
geom_point() +
theme(legend.position="bottom") +
guides(color=guide_legend(nrow=2))
无论....
如果您在上述代码的图形输出中注意到,即使我可以控制图例的“尺寸”(即行数),我也不知道如何更改 <图例的 em>顺序从垂直到水平。
- 所以
a
不是 aboveb
等(按“垂直”排序),我想要将b
添加到a
之后(按“水平
”排序)。
如何让我的图例添加对象水平而不是垂直?
像这样:
最佳答案
尝试将 byrow = TRUE
添加到 guide_legend
:
ggplot(dat, aes(x = val, y = val, color = plot)) +
geom_point() +
theme(legend.position="bottom") +
guides(color=guide_legend(nrow=2, byrow = TRUE))
关于r - 如何使 ggplot 图例水平添加对象(相对于垂直),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50417230/