Saving multiple ggplots from ls into one and separate files in R

后端 未结 4 596
独厮守ぢ
独厮守ぢ 2020-12-01 00:08

I have several ggplots as objects on my ls. I want to save them as separate files (although I would also be interested to know how to save them all under 1 big file). I have

4条回答
  •  庸人自扰
    2020-12-01 00:57

    it's best to have your plots in a list

    l = mget(plots)
    

    Then you can simply print them page-by-page,

    pdf("all.pdf")
    invisible(lapply(l, print))
    dev.off()
    

    or save one plot per file,

    invisible(mapply(ggsave, file=paste0("plot-", names(l), ".pdf"), plot=l))
    

    or arrange them all in one page,

    ggsave("arrange.pdf", arrangeGrob(grobs = l))
    

    or arrange them 2x2 in multiple pages,

    ggsave("arrange2x2.pdf", marrangeGrob(grobs = l, nrow=2, ncol=2))
    

    etc.

    (untested)

提交回复
热议问题