在r的一页上显示ggplot的多个图 [英] show multiple plots from ggplot on one page in r

查看:348
本文介绍了在r的一页上显示ggplot的多个图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个循环中制作多个ggplot并将它们显示在一个图上.

I want to make multiple ggplot in a loop and show them on one plot.

for ( i in 1:8) {
    g <- ggplot(data=mtcars, aes(x=hp, y=wt))+
        geom_point()
    print(g)
}

我想将以上图解安排在一页,4行和2列上.有谁知道这是怎么做到的吗?谢谢.

I want to arrange the plots above on one page, 4 rows and 2 columns. Does anyone know how to do that? Thanks.

推荐答案

您可以将所有图解保存在list中,然后使用cowplot::plot_grid()gridExtra::marrangeGrob()将它们放在一页或多页中

You can save all the plot in a list then use either cowplot::plot_grid() or gridExtra::marrangeGrob() to put them in one or more pages

另请参阅:

for循环中有多个图

library(tidyverse)

# create a list with a specific length 
plot_lst <- vector("list", length = 8)

for (i in 1:8) {
  g <- ggplot(data = mtcars, aes(x = hp, y = wt)) +
    geom_point()
  plot_lst[[i]] <- g
}

# Combine all plots
cowplot::plot_grid(plotlist = plot_lst, nrow = 4)

library(gridExtra)
ml1 <- marrangeGrob(plot_lst, nrow = 2, ncol = 2)
ml1

reprex软件包(v0.2.1.9000)创建于2018-09-20

Created on 2018-09-20 by the reprex package (v0.2.1.9000)

这篇关于在r的一页上显示ggplot的多个图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆