Search code examples
rggplot2cowplot

Align plot with different axes vertically using Cowplot


I am trying to align three plots (with different scales on the y-axis) on the left y-axis. In other words, I would like the red axis to be aligned:

enter image description here

However, the y-axis of the first plot does not align with the y-axis of the bottom left plot.

Code

# Libraries
library(tidyverse)
library(cowplot)

df1 <- data.frame(x = seq(0, 100, 1), 
                  y = seq(100, 0, -1))

df2 <- data.frame(x = seq(0, 10, 0.1), 
                  y = seq(1, 10^9, length.out = 101 ) )

p1 <- ggplot(data = df1) + 
  geom_line(aes(x = x, y = y))

p2 <- ggplot(data = df2) + 
  geom_line(aes(x = x, y = y))

combi_p2 <- plot_grid(p2, p2, nrow = 1)
plot_grid(p1, combi_p2, ncol = 1, axis = "l", align = "v")

Attempt to fix it

Using the information provided here, I rewrote the last part of the code:

require(grid)                    # for unit.pmax()
p1 <- ggplotGrob(p1)             # convert to gtable
combi_p2 <- ggplotGrob(combi_p2) # convert to gtable

p1.widths <- p1$widths[1:3]      # extract the first three widths, 
                                 # corresponding to left margin, y lab, and y axis
combi_p2.widths <- combi_p2$widths[1:3]                 # same for combi_p2 plot
max.widths <- unit.pmax(p1.widths, combi_p2.widths)     # calculate maximum widths
p1$widths[1:3] <- max.widths                            # assign max. widths to p1 gtable
combi_p2$widths[1:3] <- max.widths                      # assign max widths to combi_p2 gtable

# plot_grid() can work directly with gtables, so this works
plot_grid(p1, combi_p2, labels = "AUTO", ncol = 1)

Sadly, I was not able to fix the alignment:

enter image description here

Question

How do I align the y-axis of the top plot with the left bottom plot using cowplot in R?


Solution

  • A cowplot solution by Claus O. Wilke is presented here.

    It is based on the align_plot function, which first aligns the top plot with the left bottom plot along the y-axis. Then the aligned plots are passed to the plot_grid function.

    # Libraries
    library(tidyverse)
    library(cowplot)
    
    df1 <- data.frame(x = seq(0, 100, 1), 
                      y = seq(100, 0, -1))
    
    df2 <- data.frame(x = seq(0, 10, 0.1), 
                      y = seq(1, 10^9, length.out = 101 ) )
    
    p1 <- ggplot(data = df1) + 
      geom_line(aes(x = x, y = y))
    
    p2 <- ggplot(data = df2) + 
      geom_line(aes(x = x, y = y))
    
    plots <- align_plots(p1, p2, align = 'v', axis = 'l')
    bottom_row <- plot_grid(plots[[2]], p2, nrow = 1)
    
    plot_grid(plots[[1]], bottom_row, ncol = 1)
    

    enter image description here