2

I have several plots with lengthy legends: (example plot using iris, NOT my actual plot)

library(tidyverse)
library(patchwork)

theme_template <- theme_classic() +
  theme( # add border all around plot
    panel.border = element_rect(colour = "black", fill = NA, linewidth = 1)
  )

p1 <- iris %>%
  ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  scale_color_discrete(
    breaks = c("setosa", "versicolor", "virginica"),
    labels = c("Setosa", "Versicolor", "VirginicaVirginicaVirginicaVirginica")
  ) +
  theme_template

p2 <- iris %>%
  ggplot(aes(x = Petal.Length, y = Petal.Width, color = Species)) +
  geom_point() +
  theme_template

p3 <- iris %>%
  ggplot(aes(x = Species, y = Sepal.Length)) +
  geom_violin(aes(fill = Species), scale = "width") +
  geom_boxplot(width = .2, outlier.color = NA, fill = "black") +
  theme_template +
  theme(
    legend.position = "none",
    aspect.ratio = 1
  )

p4 <- iris %>%
  ggplot(aes(x = Species, y = Petal.Width)) +
  geom_violin(aes(fill = Species), scale = "width") +
  geom_boxplot(width = .2, outlier.color = NA, fill = "black") +
  theme_template +
  theme(
    legend.position = "none",
    aspect.ratio = 1
  )

p1 + p2 + p3 + p4 + plot_layout(ncol = 2, heights = c(1, 1, 1, 1))

When put together using patchwork or cowplot, it always ends like this:

A grid of four plots, every outline aligning with each other

Because of the lengthy legend, there's a huge gap between two bottom plots. Due to limitation of my actual dataset and formatting code, I'm not allowed to group the legends together or put them elsewhere not so hindering, and I can't make the wording any shorter.

I wish to achieve something like this:

A grid of four plots, only the outer boundaries were aligned

Is it possible?

ErwinTATP
  • 162
  • 6
  • Does this answer your question? [Align multiple plots in ggplot2 when some have legends and others don't](https://stackoverflow.com/questions/41569817/align-multiple-plots-in-ggplot2-when-some-have-legends-and-others-dont) – benson23 Apr 12 '23 at 06:00
  • @benson23 sadly it doesn't. I can probably make right border of bottom left graph align with the rightmost reach of top left plot legend. But that's not what I wanted. – ErwinTATP Apr 12 '23 at 07:57
  • Does adding `theme( legend.position = "bottom", legend.direction = "vertical", aspect.ratio = 1 )` to plots 1 and 2 help? – Peter Apr 12 '23 at 10:13
  • @Peter It certainly helps and looks good for me. Unfortunately, the original plots are meant for journals, and some reviewers are annoyed to find legends down there. So unless I could came up with a really really well-written response, I would like a solution with the legends stay the way as they were. – ErwinTATP Apr 13 '23 at 06:23

1 Answers1

1

If you're willing to sacrifice the aspect ratio, you can use (p1 | p2) / (p3 | p4).

library(tidyverse)
library(patchwork)

theme_template <- theme_classic() +
  theme( # add border all around plot
    panel.border = element_rect(colour = "black", fill = NA, linewidth = 1)
  )

p1 <- iris %>%
  ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  scale_color_discrete(
    breaks = c("setosa", "versicolor", "virginica"),
    labels = c("Setosa", "Versicolor", "VirginicaVirginicaVirginicaVirginica")
  ) +
  theme_template

p2 <- iris %>%
  ggplot(aes(x = Petal.Length, y = Petal.Width, color = Species)) +
  geom_point() +
  theme_template

p3 <- iris %>%
  ggplot(aes(x = Species, y = Sepal.Length)) +
  geom_violin(aes(fill = Species), scale = "width") +
  geom_boxplot(width = .2, outlier.color = NA, fill = "black") +
  theme_template +
  theme(
    legend.position = "none"
  )

p4 <- iris %>%
  ggplot(aes(x = Species, y = Petal.Width)) +
  geom_violin(aes(fill = Species), scale = "width") +
  geom_boxplot(width = .2, outlier.color = NA, fill = "black") +
  theme_template +
  theme(
    legend.position = "none"
  )

(p1 | p2) / (p3 | p4)

Created on 2023-04-12 by the reprex package (v2.0.1)

teunbrand
  • 33,645
  • 4
  • 37
  • 63
  • Yeah, I also figured out it could be realized if I ditch the `aspect.ratio` arguments. Can't understand why. – ErwinTATP Apr 13 '23 at 06:13
  • I think the following documentation might clarify some: https://patchwork.data-imaginist.com/articles/guides/layout.html#fixed-aspect-plots – teunbrand Apr 13 '23 at 08:03