Combine multiple ggplots into one plot with shared gradient legend

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.

Add Comment
1 Answer(s)

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

Answered on August 30, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.