ggplot, ggarrange - 几个情节一个传说

ggplot, ggarrange - several plots one legend

我正在绘制两个图来显示同一数据集的两个不同方面。

profile=ggplot(active,aes(distance,z,group=arm))
profile=profile+geom_line(aes(color=arm),size=0.5)
profile=profile+ggtitle(paste('Configuration',v))+guides(color="none")
slope=ggplot(active,aes(distance,slope,group=arm))
slope=slope+geom_line(aes(color=arm),size=1)
figure=ggarrange(profile, slope, 
      labels = c("A", "B"),
      ncol = 1, nrow = 2)

这使得绘图相互堆叠,因此很容易比较两个数据集,但会出现冗余图例。

我可以通过删除 +guides(color="none") 来删除第一个图上的图例 但是上面的图变宽了,x 轴不再匹配。 我如何绘制上半部分,就好像传说在那里一样?最好我想要这样的东西,那怎么办? (该示例是使用 gimp 手动制作的,因此绘图应该对齐)

由于我没有您的 active 数据集,因此我不能保证以下内容有效。

ggpubr

library(tidyverse)
library(ggpubr)

figure = ggpubr::ggarrange(profile, slope, 
                           labels = c("A", "B"),
                           ncol = 1, nrow = 2,
                           common.legend = T)

拼凑

library(tidyverse)
library(patchwork)

(profile / slope) + plot_layout(guides = 'collect') + 
  plot_annotation(tag_levels = 'A')