r语言ggplot2一夜多图_R成精系列-一页多图

本文介绍了如何在R语言中使用多种方法实现一页显示多个图表,包括multiplot函数、ggpubr包的ggarrange、gridExtra包的grid.arrange、cowplot包的plot_grid以及基础的par和layout函数。这些方法提供了灵活的布局选项,适用于创建出版级别的图形。
摘要由CSDN通过智能技术生成

在许多时候,我们需要将多张图表放在一起,当然我们可以用PS来处理,但是作为一个R的Master,怎么可能容忍这种事情发生。本文总结了如何在一页放多张绘画,供大家参考。在网友的提醒下,从新梳理了方法。

方法一:multiplot function

用函数来处理,但是效果不是很好。

This is the definition of multiplot. It can take any number of plot objects as arguments, or if it can take a list of plot objects passed to plotlist.

# Multiple plot function

#

# ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)

# - cols: Number of columns in layout

# - layout: A matrix specifying the layout. If present, 'cols' is ignored.

#

# If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),

# then plot 1 will go in the upper left, 2 will go in the upper right, and

# 3 will go all the way across the bottom.

#

multiplot

library(grid)

# Make a list from the ... arguments and plotlist

plots

numPlots = length(plots)

# If layout is NULL, then use 'cols' to determine layout

if (is.null(layout)) {

# Make the panel

# ncol: Number of columns of plots

# nrow: Number of rows needed, calculated from # of cols

layout

ncol = cols, nrow = ceiling(numPlots/cols))

}

if (numPlots==1) {

print(plots[[1]])

} else {

# Set up the page

grid.newpage()

pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))

# Make each plot, in the correct location

for (i in 1:numPlots) {

# Get the i,j matrix positions of the regions that contain this subplot

matchidx

print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,

layout.pos.col = matchidx$col))

}

}

}

方法二:用ggpubr包

ggpubr包的全称为'ggplot2' Based Publication Ready Plots,基于ggplot2的出版级图形制作包。

Hadley Wickham创建的可视化包ggplot2可以流畅地进行优美的可视化,但是如果要通过ggplot2定制一套图形,尤其是适用于杂志期刊等出版物的图形,对于那些没有深入了解ggplot2的人来说就有点困难了,ggplot2的部分语法是很晦涩的。为此Alboukadel Kassambara创建了基于ggplot2的可视化包ggpubr用于绘制符合出版物要求的图形。

ggpubr的主要特点:

- ggpubr改写了一些ggplot的图形函数,省去了ggplot中许多晦涩的语法,对于初学者来说使用起来更容易。

-能够让非高级R用户绘制准出版级的精美图形;

- 更简单地设置图形的参数,包括颜色、标签;

- 轻松实现一页多图,添加图形注释;

- 为条形图、箱线图、线图等自动添加p值和显著性水平;

- ……

先放一张图

一页放多图

#安装ggpubr

library(ggpubr)

#未安装请用install.packages("ggpubr")安装

library(ggplot2)

library(tibble)

#生成图形数据

x

y

n

z

data

#生成p1-p6六个ggplot图形对象

p1

p2

p3

p4

p5

p6

#利用ggarrange将图像排列在一起

ggarrange(p1,p2,p3,p4,p5,p6,ncol = 2,nrow =3,widths = c(1,2),heights = c(1,1,2))

解读

一页放置多张图主要利用ggarrange()函数:

ggarrange(..., plotlist = NULL, ncol = NULL, nrow = NULL, labels = NULL,label.x = 0, label.y = 1, hjust = -0.5, vjust = 1.5,

font.label = list(size = 14, color = "black", face = "bold", family = NULL),

align = c("none", "h", "v", "hv"), widths = 1, heights = 1,

legend = NULL, common.legend = FALSE)

...需要画的图对象,plotlist需要画的图清单,ncol和nrow是要排列成几行几列

labels每个图的标签,label.x label.y hjust vjust font.label都是调整labels的位置参数,

align各个图的排列方式,是纵向还是横向排列,widths和heights调整各图的长宽比列

legend是各图的图例,commn.legend公共的图例

函数能画 "ggplot", "gtable", "grob", "recordedplot"等类, 或图形函数。

方法三:用gredExtra包grid.arrange函数

library(gridExtra)

#安装ggpubr时,会依赖于该包

grid.arrange(p1,p2,p3,p4,p5,p6,ncol=3,nrow=2)

方法四:用cowplot包的plot_grid函数

library("cowplot")

plot_grid(p1,p2,p3,p4,p5,p6, labels = c("A", "B", "C","D","E","F"), ncol = 2, nrow = 3)

方法五: R的基础绘图函数par()和layout()函数

许多时候我们会舍本逐末,追求一些高级的函数而忘记了R的基础绘图函数。对于R的基础画图函数画的图可以用par和layout处理。

par用于设置简单的几行几列,参数简单。

layout同过一个参数矩阵,构造图形布局。

同时同过width和height参数控制布局的比例,实现图形的任意布置。

首先将一幅图进行切块。

然后构建布局矩阵。

设置长宽比例。

最后填充图形。

par画出的结果

布局矩阵

layout结果

attach(mtcars)

par(mfrow=c(3,2))

hist(wt)

hist(mpg)

hist(disp)

hist(carb)

hist(wt)

hist(mpg)

#用layout函数,将整张图切成9块,3*3矩阵,12放一张图,3和6放一张,89放一张,4和5 单独一张图;

#构造布局矩阵

mat

mat

layout(mat)

hist(wt)

hist(mpg)

hist(disp)

hist(carb)

hist(wt)

上面的的方法存在一个问题,不同的函数画出的图没法放在一个页面里面。

方法六:grid包viewport函数

这里就会用到grid包,grid包以前是第三方包,随着其重要性,grid随着R包直接打包下载。grid包的核心是viewport,viewport中又可以嵌套viewport,多个viewport可以构成一颗树,详细的用法,我后续再出一篇博文介绍。

针对本文的例子我们用viewport将p1-p6。

grid一页多图

#新建一个绘图

grid.newpage()

#构建布局举证

l1

#主节点

vpmain

pushViewport(vpmain)

print(p1, vp=viewport(layout.pos.row=2:3, layout.pos.col=1:2))

print(p2, vp=viewport(layout.pos.row=1, layout.pos.col=1:2))

print(p3, vp=viewport(layout.pos.row=2:3, layout.pos.col=3))

print(p4, vp=viewport(layout.pos.row=1, layout.pos.col=3))

print(p5, vp=viewport(layout.pos.row=1:3, layout.pos.col=4))

print(p6, vp=viewport(layout.pos.row=4, layout.pos.col=4))

sessionInfo()信息

sessionInfo()

R version 3.5.1 (2018-07-02)

Platform: x86_64-apple-darwin15.6.0 (64-bit)

Running under: macOS High Sierra 10.13.6

Matrix products: default

BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib

LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:

[1] zh_CN.UTF-8/zh_CN.UTF-8/zh_CN.UTF-8/C/zh_CN.UTF-8/zh_CN.UTF-8

attached base packages:

[1] grid parallel stats4 stats graphics grDevices utils datasets methods

[10] base

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值