Centered X-axis label for muliplot using cowplot package

后端 未结 2 1595
北恋
北恋 2021-02-06 04:52

I have a multiplot figure consisting of 4 plots in a 2x2 configuration. I arranged the plots using the \"cowplot\" package and the plot_grid function using the code below

相关标签:
2条回答
  • 2021-02-06 05:22

    Another option is to use textGrob to add annotations for common x and y labels.

    In this example, if you wish to remove the individual axis labels, just include theme(axis.title = element_blank()) in the plot calls. (or, for just y-axis, use theme(axis.title.y=element_blank())).

    library(ggplot2)
    library(cowplot)
    library(grid)
    library(gridExtra)
    
    ToothGrowth$dose <- as.factor(ToothGrowth$dose)
    
    #make 4 plots
    
    p1<-ggplot(ToothGrowth, aes(x=dose, y=len)) + 
      geom_boxplot()
    
    
    p2<-ggplot(ToothGrowth, aes(x=dose, y=supp)) + 
      geom_boxplot()
    
    p3<-ggplot(ToothGrowth, aes(x=supp, y=len)) + 
      geom_boxplot()
    
    p4<-ggplot(ToothGrowth, aes(x=supp, y=dose)) + 
      geom_boxplot()
    
    #combine using cowplot
    
    plot<-plot_grid(p1, p2, p3, p4, align='vh', vjust=1, scale = 1)
    
    #create common x and y labels
    
    y.grob <- textGrob("Common Y", 
                       gp=gpar(fontface="bold", col="blue", fontsize=15), rot=90)
    
    x.grob <- textGrob("Common X", 
                       gp=gpar(fontface="bold", col="blue", fontsize=15))
    
    #add to plot
    
    grid.arrange(arrangeGrob(plot, left = y.grob, bottom = x.grob))
    

    0 讨论(0)
  • 2021-02-06 05:34

    This is a little late but I had the same problem so hopefully the next person to view this question will find this useful.

    library(cowplot)
    plot<-plot_grid(p1, p2, p3, p4, align='vh', vjust=1, scale = 1)
    ggdraw(add_sub(plot, "Label", vpadding=grid::unit(0,"lines"),y=6, x=0.5, vjust=4.5))
    
    0 讨论(0)
提交回复
热议问题