1

I have multiple ggplots, currently they have different gradient legends. I want to use the grid extra package to create a plot containing all plots such as in Add a common Legend for combined ggplots

However, that solution applies to grouped discrete data. Is there a way to make this apply to my gradient legend.

j.doe
  • 35
  • 5
  • [Here's a nice vignette](https://wilkelab.org/cowplot/articles/shared_legends.html) about doing this using `extract_legend` from the `cowplot` package. If you need more "hands on" help, you will need to post a reproducible example for us to assist you. – Allan Cameron Aug 25 '20 at 11:28

1 Answers1

2

Without more information (for instance current code along with dput output), it's very difficult to answer your question.

However, based on your speech only, the {patchwork} package (link) seems best suited for this kind of operation.

For instance, you could write this:

library(tidyverse)
library(patchwork)

p1 <- ggplot(mtcars) + geom_point(aes(mpg, disp, color=hp))
p2 <- ggplot(mtcars) + geom_point(aes(drat, wt, color=hp))

p1 + p2

enter image description here

p1 + p2 + plot_layout(guides = 'collect')

enter image description here

Dan Chaltiel
  • 7,811
  • 5
  • 47
  • 92
  • Just as a sidenote, the two legends from the plots you are combining should have the exact same limits, breaks, labels, name etc. otherwise they won't be collected. – teunbrand Aug 25 '20 at 12:26