1

我使用 for 循环创建了许多图ggplot,并将每个图保存到一个列表中plots

plots <- list()

for (i in 1:238)

{

    gene <- row.names(geneExpression)[i]

    df.sub <- df[ , c("source", gene)]

    names(test.sub) <- c("source", "exp")

    plots[[i]] <- ggplot() + geom_violin(data=test.sub, aes(source, exp, fill=source, color=source), alpha=.4, trim=F, environment = environment()) + coord_flip() + ggtitle(gene) + theme(legend.position="none") + labs(x="")

}

我正在使用其他地方建议的 gridExtra 函数,但是当我这样做时,它会在一个页面中打印所有图(240 个图)。

pdf("violinPlots.pdf")
do.call(grid.arrange, plots)
dev.off()

有没有办法可以指定我想要每页 24 个图?(即 6 行 x 4 列?)

我试过这个,但它返回一个错误......

grid.arrange(plots, ncol=4, newpage = T )
4

1 回答 1

2

您可以使用 gridExtra 的marrangeGrob函数:

pdf("violinPlots.pdf")
ml <- marrangeGrob(grobs = plots, nrow = 6, ncol = 4)
print(ml)
dev.off()
于 2015-04-16T17:58:38.037 回答