Warm tip: This article is reproduced from stackoverflow.com, please click
ggplot2 plot r egg

Aligning multiple plot in egg::ggarrange

发布于 2020-04-16 12:34:59

I'm making a multiplot with egg::ggarrange for 5 figures. I'm wondering how can I aligning plot area of b,d and c,e in vertical position?

PS: Figure b and c have to be in different decimal in y axis.

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

enter image description here

Questioner
Jellz
Viewed
10
Allan Cameron 2020-02-04 20:51

You could just make the y scales equal, or at least have the same number of decimal points:

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

enter image description here