Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

specify specific position for collected legend in patchwork

I am using the patchwork package in r to create panels of plots like:

panel<- (p1+  plot_spacer()+p2 + p3)+
    plot_layout(ncol = 2) +
  plot_layout(guides = "collect")
panel

enter image description here

I want to specify the legend to go to the empty top-right panel, more or less like this enter image description here

Appreciate any pointers

like image 426
Myriad Avatar asked Sep 11 '25 18:09

Myriad


1 Answers

For this use case patchwork provides guide_area() which could be used to place the legend:

library(patchwork)
library(ggplot2)

p1 <- p2 <- p3 <- ggplot(mtcars, aes(hp, mpg, color = factor(cyl))) +
  geom_point()

p1 + guide_area() + p2 + p3 +
  plot_layout(ncol = 2) +
  plot_layout(guides = "collect")

like image 76
stefan Avatar answered Sep 13 '25 07:09

stefan