Search code examples
rggplot2r-grid

ggplot - Same x-axis length for aligned plots with 2 and 3 y-axis


I wish to align a 3-y-axis plot with a 2-y-axis plot so that x-axis' lengths would be identical.

But, owing to the third axis of the first plot, x-axis' lengths are different among plots.

library(ggplot2)
library(grid)
library(patchwork)

My data :

data_1 <- data.frame(x = c(1,2,3,4),
               y1 = c(5,8,1,6),
               y2 = c(90,40,70,50),
               y3 = c(600,800,900,1000))

data_2 <- data.frame(x = c(1,2,3,4),
                 y1 = c(9,4,2,1),
                 y2 = c(45,70,20,10))

My first plot (done with data_1) :

 plot_1a <- ggplot(data = data_1, aes(x = x)) +

  geom_line(aes(y = y1, color = "y1")) + 

  scale_y_continuous(sec.axis = sec_axis(trans = ~ ./ 0.09, name = "y2")) + 

  geom_line(aes(y = y2 * 0.09, color = "y2")) + 

  geom_line(aes(y = y3 * 0.008, color = "y3")) + 

  scale_color_manual(values = c("y1" = "red", "y2" = "green", "y3" = "blue")) +

  guides(colour = FALSE) +

  theme(axis.text.y.left = element_text(color = "red"), 
        axis.ticks.y.left = element_line(color = "red"),
        axis.line.y.left = element_line(color = "red"),
        axis.title.y.left = element_text(color = "red"),

        axis.text.y.right = element_text(color = "green"), 
        axis.ticks.y.right = element_line(color = "green"),
        axis.line.y.right = element_line(color = "green"),
        axis.title.y.right = element_text(color = "green"))


plot_1b <- ggplot(data = data_1, aes(x = x)) +

  scale_y_continuous(sec.axis = sec_axis(trans = ~ . / 0.008, name = "y3")) +

  labs(x = "", y = "") +

  theme(axis.text.y.right = element_text(color = "blue"), 
        axis.ticks.y.right = element_line(color = "blue"),
        axis.line.y.right = element_line(color = "blue"),
        axis.title.y.right = element_text(color = "blue") ,

        axis.text.y.left = element_blank(),
        axis.line.y.left = element_blank(),
        axis.ticks.y.left = element_blank(),

        axis.line.x = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),

        panel.background = element_blank(),
        plot.margin = margin(1, 0, 1, 1, "mm"))

plot_1 <- plot_1a + plot_1b + plot_layout(ncol = 2,  widths = c(1,1e-3))

My second plot (done with data_2):

   plot_2 <- ggplot(data = data_2, aes(x = x)) +

  geom_line(aes(y = y1, color = "y1")) + 

  scale_y_continuous(sec.axis = sec_axis(trans = ~ ./ 0.09, name = "y2")) + 

  geom_line(aes(y = y2 * 0.09, color = "y2")) + 

  scale_color_manual(values = c("y1" = "red", "y2" = "green")) +

  guides(colour = FALSE) +

  theme(axis.text.y.left = element_text(color = "red"), 
        axis.ticks.y.left = element_line(color = "red"),
        axis.line.y.left = element_line(color = "red"),
        axis.title.y.left = element_text(color = "red"),

        axis.text.y.right = element_text(color = "green"), 
        axis.ticks.y.right = element_line(color = "green"),
        axis.line.y.right = element_line(color = "green"),
        axis.title.y.right = element_text(color = "green"))

I wish to align them vertically. So I do :

vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y)
grid.newpage()

pushViewport(viewport(layout = grid.layout(2, 1)))

print(plot_1, vp = vplayout(1, 1))
print(plot_2, vp = vplayout(2, 1))

But it returns me both plots that are not aligned as I want. I want the red and green y-axis vertically aligned (i.e. x-axis' lengths are identical).


Solution

  • You may want to consider submitting a feature request to the maintainers of patchwork asking for the ability to lay things out in nxn grids. I played around with it since it would be the simplest solution but it doesn't seem possible.

    Here is one solution using grid.arrange from gridExtra and ggplotGrob from ggplot2:

    library(gridExtra)
    p <- rbind(ggplotGrob(plot_1a), ggplotGrob(plot_2), size="last")
    q <- ggplotGrob(plot_1b)
    q$heights <- p$heights
    grid.arrange(p,q,widths=c(8,1))
    

    Adjust widths in grid.arrange as desired.

    aligned axes with grid.arrange