2

I have a question concerning plot alignment in Cowplot.

I would like to align plots (ggplot) in a panel consisting of 3 columns. The first column has 2 plots (vertical aligned), the second column 1 plot, and the third also two plots (vertical aligned).

Example:

# Packages
library(tidyverse)
library(cowplot)

# Create sample data
df <- data.frame(replicate(2,sample(0:10,10,rep=TRUE)))

# Create sample plots
plot.a1 <- ggplot(df, aes(x=df$X1, y=df$X2)) +
  geom_point()

plot.a2 <- plot.a1
plot.a <- plot_grid(plot.a1, plot.a2, align = "v", ncol = 1, nrow = 2)

plot.b <- plot.a1

plot.c1 <- plot.a1
plot.c2 <- plot.a1
plot.c <- plot_grid(plot.c1, plot.c2, align = "v", ncol = 1, nrow = 2)

# Create panel figure
plot_grid(plot.a, plot.b, plot.c, labels = c("A", "B", "C"), align = "h", axis = "b", nrow = 1, ncol = 3)

I expected that by aligning the panel horizontally (align = "h", axis = "b") the bottom axis of the plot would align correctly, however I am doing something wrong.

How can I align the columns horizontally (by the bottom axis)?

user213544
  • 2,046
  • 3
  • 22
  • 52
  • 1
    If you just omit `align = "h"` from your code, does that give you what you want? – Dan Oct 18 '18 at 16:37
  • Probably won't solve the problem, but take a look at [this post](https://stackoverflow.com/q/32543340/5325862) in the r-faq tag on why you should basically never be using `$` inside `aes` – camille Oct 18 '18 at 17:21

1 Answers1

5

plot_grid is making its own margins for the resulting plot panel.

If you need plot.b to align with the others, just call:

plot.b <- plot_grid(plot.a1)

Even though it is empty. Then, the last plot_grid() call is passed three element, all of which are the same.

plot_grid(plot.a, plot.b, plot.c, labels = c("A", "B", "C"), align = "h", axis = "b", nrow = 1, ncol = 3)

enter image description here

Nate
  • 10,361
  • 3
  • 33
  • 40