0

I have a set of 3 scatterplots with a common legend that I'm making in R, does anyone know how to make a legend as its own separate figure?

Here's what I have so far:

library(ggplot2)
library(gridGraphics)
plot_grid(lymph_plot, imm_plot, fibro_plot, labels = c("A", "B", "C"), ncol = 3, nro1 = 1)

edit: Thank you Melissa Key, I was able to figure it out. I got the legend from the last scatterplot and used it as the last final element of the plot grid. Here's the code:

legend <- cowplot::get_legend(fibro_plot)

fibro_plot <- fibro_plot + theme(legend.position = "none")

plot_grid(lymph_plot, imm_plot, fibro_plot, legend, labels = c("A", "B", "C"), 
          rel_widths = c(1, 1, 1, 1/3), ncol = 4, nrow = 1)
fgootkind
  • 73
  • 6
  • 3
    Check out `cowplot::get_legend` – Melissa Key Jun 20 '23 at 16:24
  • 2
    Alternatively, if you are trying to group your 3 scatterplots together with 1 common legend, you might find the `patchwork` package to be useful https://patchwork.data-imaginist.com/ – Melissa Key Jun 20 '23 at 16:26
  • 1
    Does this answer your question? [Add a common Legend for combined ggplots](https://stackoverflow.com/questions/13649473/add-a-common-legend-for-combined-ggplots) – SamR Jun 20 '23 at 16:29
  • 1
    See [How to incorporate legend as plot of plot size when arranging multiple plots using patchwork?](https://stackoverflow.com/questions/69284225/how-to-incorporate-legend-as-plot-of-plot-size-when-arranging-multiple-plots-usi/69284489#69284489) for both a `patchwork` and a `cowplot` approach as suggested by Melissa Key. – stefan Jun 20 '23 at 16:31
  • Thanks everyone, I wound up just getting the legend using cowplot::get_legend (thank you Melissa) and using it as the 4th part of a figure. – fgootkind Jun 20 '23 at 21:16

0 Answers0