如何更改 ggplot2 图例中每个项目的方向?

How to change orientation of each item in a ggplot2 legend?

上面显示了 2 个图例,一个是每个项目都放在一个新列中,另一个是每个项目都放在一个新行中。

我该如何控制?

这由 nrowncol 参数控制 guide_legend()

您可以在 guides():

内调用 guide_legend()
plot + 
  guides(color = guide_legend(nrow = 1))

...或在 scale_*():

plot +
  scale_color_discrete(guide = guide_legend(nrow = 1))