ggplot - 具有 2 和 3 y 轴的对齐图的相同 x 轴长度

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

我希望将 3 轴图与 2 轴图对齐,以便 x 轴的长度相同。

但是,由于第一个图的第三个轴,x 轴的长度在图之间是不同的。

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

我的数据:

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

我的第一个情节(用 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))

我的第二个情节(用 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"))

我想垂直对齐它们。所以我这样做:

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

但是 returns 我的两个图都没有按照我想要的方式对齐。我希望红色和绿色的 y 轴垂直对齐(即 x 轴的长度相同)。

您可能需要考虑向 patchwork 的维护者提交功能请求,要求能够在 nxn 网格中进行布局。我试过它,因为它是最简单的解决方案,但它似乎不可能。

这是一个使用 gridExtragrid.arrangeggplot2ggplotGrob 的解决方案:

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

根据需要调整 grid.arrange 中的 widths