在r中的一页上显示来自ggplot的多个图 [英] show multiple plots from ggplot on one page in r
本文介绍了在r中的一页上显示来自ggplot的多个图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在一个循环中制作多个 ggplot 并将它们显示在一个图上.
for ( i in 1:8) {g <- ggplot(data=mtcars, aes(x=hp, y=wt))+geom_point()打印(克)}
我想将上面的图表排列在一页 4 行 2 列上.有谁知道这是怎么做到的吗?谢谢.
解决方案
您可以将所有绘图保存在 list
中,然后使用 cowplot::plot_grid()
或gridExtra::marrangeGrob()
将它们放在一页或多页
另见:
(v0.2.1.9000) 于 2018 年 9 月 20 日创建
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) }
I want to arrange the plots above on one page, 4 rows and 2 columns. Does anyone know how to do that? Thanks.
解决方案You can save all the plot in a
list
then use eithercowplot::plot_grid()
orgridExtra::marrangeGrob()
to put them in one or more pagesSee also:
Creating arbitrary panes in ggplot2 (
patchwork
,multipanelfigure
&egg
packages)
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
Created on 2018-09-20 by the reprex package (v0.2.1.9000)
这篇关于在r中的一页上显示来自ggplot的多个图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文