【问题标题】:save multiple plots (from ggplot2) using a for-loop by side-by-side使用for循环并排保存多个图(来自ggplot2)
【发布时间】:2018-02-19 22:28:34
【问题描述】:

我像这样将 ggplot 保存在 for 循环中。我总共有大约 300 个地块 (i = 1,,,300)。我想用pdf 文件保存这些图,但在 pdf 文件的一页(2 x 2)中保存 4 个图。如果是这样,pdf 文件的输出应该有 75 页。

for( i in 1:300){ plot_list[[i]] = ggplot(out, aes(basket_size_group_by2dol, pct_trips_w_item, colour=channel2)) + ylim(min00,max00) + geom_point() }

【问题讨论】:

    标签: r plot ggplot2


    【解决方案1】:

    由于您已生成绘图列表,因此您可以使用 ggplot2 包中的 ggsave 函数和 gridExtra 包中的 marrangeGrob 函数生成 pdf。这是一个小例子,它生成八个图并将它们保存在一个两页的 pdf 中,四个到一页。

    library(ggplot2)
    library(gridExtra)
    
    plot_list <- vector("list", 8) 
    plot_list[[1]] <- ggplot(mtcars) + aes(x = wt, y = mpg)   + geom_point() + ggtitle("This is plot 1")
    plot_list[[2]] <- ggplot(mtcars) + aes(x = cyl, y = mpg)  + geom_point() + ggtitle("This is plot 2")
    plot_list[[3]] <- ggplot(mtcars) + aes(x = disp, y = mpg) + geom_point() + ggtitle("This is plot 3")
    plot_list[[4]] <- ggplot(mtcars) + aes(x = drat, y = mpg) + geom_point() + ggtitle("This is plot 4")
    plot_list[[5]] <- ggplot(mtcars) + aes(x = drat, y = mpg) + geom_point() + ggtitle("This is plot 5")
    plot_list[[6]] <- ggplot(mtcars) + aes(x = qsec, y = mpg) + geom_point() + ggtitle("This is plot 6")
    plot_list[[7]] <- ggplot(mtcars) + aes(x = vs, y = mpg)   + geom_point() + ggtitle("This is plot 7")
    plot_list[[8]] <- ggplot(mtcars) + aes(x = gear, y = mpg) + geom_point() + ggtitle("This is plot 8")
    
    
    ### Use your plot_list here:
    glist <- lapply(plot_list, ggplotGrob)
    ggsave("plots.pdf", marrangeGrob(glist, nrow = 2, ncol = 2))
    

    【讨论】:

    • 非常感谢。另外,有没有什么方法可以减小比例,比如 scale = 0.8 之类的?
    • 我认为scale = 0.8 您正在寻找修改地块之间的间距?例如,您可以通过ggplot2::theme(plot.margin = unit(c(2,2,2,2), "cm")) 在 ggplot 中设置绘图边距。尝试将其添加到绘图中,生成 pdf,并根据需要调整 plot.margin
    【解决方案2】:

    我建议使用cowplotplot_grid() 函数并在网格中附加ggplot2 对象。例如。见:https://cran.r-project.org/web/packages/cowplot/vignettes/plot_grid.html

    [编辑]
    示例代码(测试代码):

    library(cowplot)
    library(ggplot2)
    dat <- data.frame(x=1:10,y=2*(1:10),z=(1:10)^2)
    p1 <- ggplot(dat, aes(x=x, y=y)) + geom_point()
    p2 <- ggplot(dat, aes(x=x, y=z)) + geom_point()
    plot_grid(p1, p2, labels=c("A","B"))
    

    以下逻辑可能允许将想法扩展到动态 for 循环:

    plotlist <- list(p1,p2)
    plot_grid(plotlist=plotlist)
    

    [EDIT2]
    这是一个使用 split 命令的小演示。我生成一个从 1 到 12 的向量 x(以相同的方式使用列表)。然后我生成我的因子 f,它定义了组(相同的数字,相同的组)。使用 split 返回 4 个 x 列表,按因子 f 分组。

    x <- 1:12
    f <- rep(seq(1,4),each=3)
    split(x,f)
    

    $1 [1] 1 2 3

    $2 [1] 4 5 6

    $3 [1] 7 8 9

    $4 [1] 10 11 12

    您可以与这个额外的代码进行比较以了解发生了什么:

    f1 <- rep(seq(1,4), times=3)
    split(x,f1)
    

    $1 [1] 1 5 9

    $2 [1] 2 6 10

    $3 [1] 3 7 11

    $4 [1] 4 8 12

    您还可以通过以下方式获得 R 函数的帮助页面:

    ?split
    ?plot_grid
    

    甚至对于包

    ?cowplot
    

    【讨论】:

    • 请查看我的编辑,您可以为 plot_grid 函数指定 plotlist 参数。如果您现在(但是......例如使用 lapply)创建一个 ggplot 对象列表,您可以将该列表转发到 plot_grid
    • 针对您的具体问题。您执行以下操作:1)创建所有绘图的绘图列表,2)split() 将列表分成 4 个绘图块,每个块 3)使用 plotlist 参数在每个块上运行 plot_grid 5)以 2by2 排列,您可以使用 ncol=2论据
    猜你喜欢
    • 2014-04-23
    • 1970-01-01
    • 2014-11-19
    • 1970-01-01
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    • 2017-04-18
    • 2021-09-23
    相关资源
    最近更新 更多