I am trying to align two panel plots, generated with a facet_wrap
in ggplot
in the following manner (note: panel A needs to stay empty):
I noticed, however, that the y-axes of panel B are not exactly aligned with the y-axes from the last to plots of panel C (despite the option axis = 'lb'
in both facets).
Code
# Load libraries
library(tidyverse)
library(cowplot)
# Create two facetted plots
p1 <- ggplot(data = diamonds, aes(x = carat, y = price)) +
facet_wrap(~ cut, scales = "free_y", ncol = 5) +
geom_point(size=0.5)
p2<- ggplot(data = filter(diamonds, price < 900 & (cut == "Fair" | cut == "Good" )), aes(x = carat, y = price)) +
facet_wrap(~ cut, scales = "free_y", ncol = 2) +
geom_point(size=0.5)
# Create panel A and panel B
a <- plot_grid(NULL, p2, labels = c("A", "B"), axis = 'lb', ncol = 2, rel_widths = c(3,2))
# Create a combined panel of 'a' and panel C
plot_grid(a, p1, labels = c("", "C"), axis = 'lb', ncol = 1, rel_heights = c(1,1))
First, I thought it had something to do with the y-axis label, but removing the label doesn't solve the issue.
Question
What is causing this behaviour and how can you align plots, generated with a facet_wrap
, using the cowplot
package.
Desired output
I would like the y-axis of panel B to be aligned vertically with the y-axis of the last two plots in panel C (i.e., at the red line)