R 在一起布置列表和打印时保持打印高度固定

R 在一起布置列表和打印时保持打印高度固定,r,ggplot2,gridextra,r-grid,grob,R,Ggplot2,Gridextra,R Grid,Grob,我正在创建一个热图的列表,这些热图的行数相同,列数不同,x轴刻度标签的长度也不同: 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(rn

我正在创建一个热图的
列表
,这些热图的行数相同,列数不同,x轴刻度标签的长度也不同:

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")
}
或使用
cowplot
plot\u网格

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

因此,我的问题是:

  • 如何使它们具有相同的高度,并使x轴标签向下延伸到不同的长度
  • 缩小他们之间的距离?我尝试减少
    填充
    值,但没有看到任何更改

  • 请注意,我知道首先使用
    facet\u grid
    可能是创建此图的明显方式,但我特别需要先创建图列表,然后再将它们组合起来。

    这两个
    egg:ggarrange
    cowplot::plot\u grid()
    都可以实现

    关于回答1,请尝试:

    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.
    
    plot_grid
    将为您提供与下面相同的图像

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

    签出
    cowplot::plot_grid()
    -此函数有一个
    align
    参数,可用于按坐标轴对齐绘图。谢谢你的建议@Jan Boyer。用我认为正确的参数值进行了尝试,最终得到了
    gridExtra::arrangeGrob
    给我的确切结果。我用这个更新了我的帖子。
    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))
    
    cowplot::plot_grid(plot1, plot2, plot3, ncol = 3, axis = "b", align = "h", rel_widths = c(600,30,300))