0

就像标题一样。我有一个情节。

p <- ggplot(mtcars) + 
  geom_point(aes(hp, wt, colour = mpg)) + 
  ggtitle('Plot 3')

当我+用来组合情节时,我可以收集传说。

p + p + plot_layout(guides = 'collect')

当我使用|组合情节时,我无法收集图例。

p | p + plot_layout(guides = 'collect')

我不知道为什么。

|和和有什么区别+,什么时候用|,什么时候用+

任何帮助将不胜感激。

4

1 回答 1

2

这是因为如何根据运算符优先级计算嵌套级别。

p + p + plot_layout(guides = 'collect')

被解释为

((p + p) + plot_layout(guides = 'collect'))

然而

p | p + plot_layout(guides = 'collect')

被解释为

p | (p + plot_layout(guides = 'collect')

而你想要

(p | p) + plot_layout(guides = 'collect')

在此处输入图像描述

于 2020-11-26T15:57:50.343 回答