3

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?

user213544
  • 2,046
  • 3
  • 22
  • 52

2 Answers2

2

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

user213544
  • 2,046
  • 3
  • 22
  • 52
1

I think you can use ggplotGrob and put them together with gtable_rbind and gtable_cbind. Finally, you can draw the plot with grid.draw()

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

g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(p2)

frame_g2 <- gtable_frame(g2 , debug = TRUE)

frame_combi <- gtable_frame(gtable_cbind(frame_g2,frame_g2),
                            width = unit(2, "null"),
                            height = unit(1, "null"))
frame_g1  <-
   gtable_frame(
     g1,
     width = unit(1, "null"),
     height = unit(1, "null"),
     debug = TRUE
    )

 grid.newpage()
 all_frames <- gtable_rbind(frame_g1, frame_combi)
 grid.draw(all_frames)

And this is how the plot looks. enter image description here

Rhino8
  • 156
  • 1
  • 7