Search code examples
rggplot2plotegg

Aligning multiple plot in egg::ggarrange


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


    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


Solution

  • 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