如何在R中自动化grid.arrange函数的这一部分

如何在R中自动化grid.arrange函数的这一部分,r,R,我有一个代码块,如下所示: p1 <- plotQC(sce_1, type = "highest-expression") p2 <- plotQC(sce_2, type = "highest-expression") p3 <- plotQC(sce_3, type = "highest-expression") p4 <- plotQC(sce_4, type = "highest-expression") grid.arrange(p1,p2,p3,p4,nco

我有一个代码块,如下所示:

p1 <- plotQC(sce_1, type = "highest-expression")
p2 <- plotQC(sce_2, type = "highest-expression")
p3 <- plotQC(sce_3, type = "highest-expression")
p4 <- plotQC(sce_4, type = "highest-expression")
grid.arrange(p1,p2,p3,p4,ncol=2)
第二个块也工作得很好。但是,我想让grid.rangework不需要手动告诉它p1、p2、p3、p4,但它应该检测到p对象的数量


我该怎么做?我在R markdown中工作。

虽然所有这些都有效,但我想您会同意,以下内容更好,因为它不需要您重复任何一行或手动指定一些数字:

sce <- list(sce_1, sce_2, sce_3, sce_4)
p <- lapply(sce, plotQC, type = "highest-expression")
do.call(grid.arrange, c(p, ncol = 2))

sce虽然所有这些都有效,但我想您会同意,以下内容更好,因为它不需要您重复任何一行或手动指定一些数字:

sce <- list(sce_1, sce_2, sce_3, sce_4)
p <- lapply(sce, plotQC, type = "highest-expression")
do.call(grid.arrange, c(p, ncol = 2))

sce如果您想保持循环,也可以尝试以下方法:

p.list <- list()
for (i in 1:length(paths)){
  p <- plotQC(get(paste0("sce_",i)), type = "highest-expression")
  p.list[[i]] <- p
}
cowplot::plot_grid(plotlist = p.list)

p.list如果您想保持循环,也可以尝试以下方法:

p.list <- list()
for (i in 1:length(paths)){
  p <- plotQC(get(paste0("sce_",i)), type = "highest-expression")
  p.list[[i]] <- p
}
cowplot::plot_grid(plotlist = p.list)

p.list谢谢您的解释。下次我会记住的。谢谢你的解释。下次我会记住的。:)