Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align facetted ggplots (facet_wrap) with cowplot in R

Tags:

r

ggplot2

cowplot

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):

enter image description here

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)

enter image description here

like image 625
user213544 Avatar asked Oct 15 '22 05:10

user213544


1 Answers

Like I said in the comments, I think it has to do with the spacing of your facets as result of the 2 vs 5 plots + the difference in Y axis labels. I could be wrong (which is pretty likely), so there might be a easier/prettier solution. Again, this is a proposal, you might play around a little with the numbers but I though this was pretty close:

# 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) +
  theme(panel.spacing = unit(1.15, "lines"),
        axis.title.y = element_text(margin = margin(t = 0, r = 5, b = 0, l = 0)))

# Create panel A and panel B
a <- plot_grid(NULL, p2, labels = c("A", "B"), axis = 'lb', ncol = 2, rel_widths = c(2.985,2.015))

# 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))

enter image description here

So basically what I added is some more spacing between your p2 graphs, as the numbers in the bottom plots take up more space than those in the top ones (there are 2 extra numbers after all). For me that fixed the alignment of the two plots on the right side. I also increased the margin of the label to the axis. This fixed the alignment for the left top plot. I am unsure whether it is desirable to change the distance between the label and axis tho.

like image 189
Annet Avatar answered Oct 28 '22 15:10

Annet