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

时间:2019-01-30 15:24:34

标签: r ggplot2 r-grid

我希望将3 y轴图与2 y轴图对齐,以使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))

但是它会返回我未按要求对齐的两个图。我希望红色和绿色的y轴垂直对齐(即x轴的长度相同)。

1 个答案:

答案 0 :(得分:1)

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

这是一种使用e.isPresent()中的e.isDisplayed()patchwork中的grid.arrange的解决方案:

gridExtra

根据需要在ggplotGrob中调整ggplot2

aligned axes with grid.arrange