thomasp85/patchwork

legend alignment

Closed this issue · 2 comments

In the example below, legends are aligned centrally for each plot. Is it possible to have legend left aligned instead? Using plot_layout(guides = 'collect') does left align the legends, but it also brings the legends in the middle of the patchwork, which makes it confusing which legend belongs to which plot, especially in a more complex design. I want to keep the legend in their original position BUT have them left aligned, and I can't seem to find a way to do it...

p1 <- ggplot(iris) + 
     geom_point(aes(Sepal.Length, Sepal.Width,fill = Petal.Length)) + 
     ggtitle('Plot 1') + labs(fill='Petal') 
 
p2 <- ggplot(iris) + 
    geom_boxplot(aes(Sepal.Length, Species,fill = Species)) + 
     ggtitle('Plot 2')

p1/p2

Thanks a million! (patchwork is really a gamechanger!!!!)

You can set the legend.justification theme element for both plots using &.

library(ggplot2)
library(patchwork)

p1 <- ggplot(iris) + 
     geom_point(aes(Sepal.Length, Sepal.Width,fill = Petal.Length)) + 
     ggtitle('Plot 1') + labs(fill='Petal') 
 
p2 <- ggplot(iris) + 
    geom_boxplot(aes(Sepal.Length, Species,fill = Species)) + 
     ggtitle('Plot 2')

p1/p2 & theme(legend.justification = "left")

Created on 2023-12-21 with reprex v2.0.2

love it. Thanks.