Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ggplot, ggarrange – several plots one legend

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading