温馨提示:本文翻译自stackoverflow.com,查看原文请点击:r - Aligning multiple plot in egg::ggarrange
ggplot2 plot r egg

r - 对齐egg :: ggarrange中的多个图

发布于 2020-04-17 15:15:17

我正在用egg :: ggarrange绘制5个图形。我想知道如何在垂直位置对齐b,d和c,e的绘图区域?

PS:图b和c在y轴上必须使用不同的小数。

    library(egg)
    library(ggplot2)

    data("ToothGrowth")
    data("mtcars")

    P1 <- ggplot(mtcars, aes(x = wt, y = mpg, color=cyl))+
          geom_point()       # Add correlation coefficient

    P2 <- ggboxplot(ToothGrowth, x = "dose", y = "len",
             color = "dose", palette = "jco")+
             scale_y_continuous(breaks=c(10.5, 20.5, 30.5))

    P3 <- ggdotplot(ToothGrowth, x = "dose", y = "len",
             color = "dose", palette = "jco", binwidth = 1)


    ggarrange(P1,
        ggarrange(P2, P2, ncol = 2, labels = c("b", "d"), align = "h",widths = c(1.5,2)), 
        ggarrange(P3, P3, ncol = 2, labels = c("c", "e"), align = "h",widths = c(1.5,2)), 
      nrow = 3, 
      heights = c(1.5, 1, 1),
      labels = "a" 
      ) 

在此处输入图片说明

查看更多

提问者
Jellz
被浏览
1
Allan Cameron 2020-02-04 20:51

您可以使y标度相等,或者至少具有相同的小数位数:

    library(egg)
    library(ggplot2)
    library(ggpubr)

    data("ToothGrowth")
    data("mtcars")

    P1 <- ggplot(mtcars, aes(x = wt, y = mpg, color=cyl))+
          geom_point()       # Add correlation coefficient

    P2 <- ggboxplot(ToothGrowth, x = "dose", y = "len",
             color = "dose", palette = "jco")+
             scale_y_continuous(breaks=c(10.5, 20.5, 30.5))

    P3 <- ggdotplot(ToothGrowth, x = "dose", y = "len",
             color = "dose", palette = "jco", binwidth = 1) +
      scale_y_continuous(breaks=c(10.5, 20.5, 30.5))


    ggarrange(P1,
        ggarrange(P2, P2, ncol = 2, labels = c("b", "d"), align = "h",widths = c(1.5,2)), 
        ggarrange(P3, P3, ncol = 2, labels = c("c", "e"), align = "h",widths = c(1.5,2)), 
      nrow = 3, 
      heights = c(1.5, 1, 1),
      labels = "a" 
      ) 

在此处输入图片说明