ggplot, ggarrange - several plots one legend

Viewed 170

I am making two plots showing two different aspects of the same data set.

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)

This makes the plots stacked on top of each other, so it is easy to compare the two datasets, but it comes out with a redundant legend.

enter image description here

I can remove the legend on the first plot by removing the +guides(color="none") But then the upper plot is wider and the x-axises does not match up any longer. enter image description here How can I plot the upper part as if the legend was there? Preferably I would like something like this, how is that doable? (the example is made manually using gimp, so the plots should line up) enter image description here

1 Answers

Since I do not have your active dataset, I cannot guarantee the following would work.

ggpubr

library(tidyverse)
library(ggpubr)

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

patchwork

library(tidyverse)
library(patchwork)

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