Search code examples
rggplot2gridextrar-gridgrob

Keeping plotted heights fixed when laying out a list ggplot together


I'm creating a list of ggplot heatmaps, which have the same number of rows but different number of columns and different lengths of their x-axis tick labels:

plot.list <- vector(mode="list",length(3))
n.cols <- c(600,30,300)
x.labs <- c("medium","this is a long label","sh")
library(ggplot2)

for(i in 1:3){
  set.seed(1)
  df <- reshape2::melt(matrix(rnorm(100*n.cols[i]),100,n.cols[i],dimnames = list(paste0("G",1:100),paste0("S",1:n.cols[i]))))
  plot.list[[i]] <- ggplot(data=df,mapping=aes(x=Var2,y=Var1,fill=value))+
    geom_tile()+theme_minimal()+scale_fill_gradient2(name="Scaled Value",low="darkblue",mid="gray",high="darkred")+
    scale_x_discrete(name=NULL,breaks=unique(df$Var2)[floor(length(unique(df$Var2))/2)],labels=x.labs[i])+
    scale_y_discrete(name=NULL)+
    theme(legend.position=NULL,axis.title.x=element_blank(),axis.text.x=element_text(angle=90,hjust=1,vjust=0.5))
  if(i != 1) plot.list[[i]] <- plot.list[[i]]+theme(axis.text.y=element_blank())
  if(i != 3) plot.list[[i]] <- plot.list[[i]]+theme(legend.position = "none")
}

I then want to combine them together horizontally with a very small margin separating them, and have their widths be relative to the numbers of columns.

Trying to achieve this using gridExtra's arrangeGrob:

gridExtra::arrangeGrob(grobs=plot.list,ncol=length(plot.list),widths=n.cols,padding=0.01)

Or with cowplot's plot_grid:

cowplot::plot_grid(plotlist=plot.list,align="v",axis="tb",ncol=length(plot.list),rel_widths=n.cols)

Gives me: enter image description here

So my questions are:

  1. How to get them to have the same height and have the x-axis labels extend downwards to varying lengths?
  2. Shrink the spaces between them? I tried reducing the padding value but see no change

Note that I know that using facet_grid might be the obvious way to create this in the first place, but I specifically need to first create the list of plots and only then combine them.


Solution

  • Both egg:ggarrange and cowplot::plot_grid() can accomplish this.

    As far as answering 1, try out:

    library(egg)
    plot1 <- plot.list[[1]]
    plot2 <- plot.list[[2]]
    plot3 <- plot.list[[3]]
    ggarrange(plot1, plot2, plot3, ncol = 3, widths = c(600,30,300)) #originally had the 20,3,10, but I don't think it scales right.
    

    As far as 2, you can set you plot.margins beforehand and arrange like before.

    plot1 <- plot.list[[1]] + theme(plot.margin = margin(1,0,1,1)) # order is top, right, bottom, left. Go negative if you want them to touch.
    plot2 <- plot.list[[2]] + theme(plot.margin = margin(1,0,1,0))
    plot3 <- plot.list[[3]] + theme(plot.margin = margin(1,1,1,0))
    ggarrange(plot1, plot2, plot3, ncol = 3, widths = c(600,30,300))
    

    plot_grid will give you the same image as below.

    cowplot::plot_grid(plot1, plot2, plot3, ncol = 3, axis = "b", align = "h", rel_widths = c(600,30,300))