r中gglot怎么组合多张图_R语言:多幅图形组合为一幅图形的方法

备注:学习备忘

在R中使用函数par()或layout()可以容易地组合多幅图形为一幅总括图形。

你可以在par()函数中使用图形参数mfrow=c(nrows, ncols)来创建按行填充的、行数为

nrows、列数为ncols的图形矩阵。另外,可以使用nfcol=c(nrows, ncols)按列填充矩阵。

> attach(mtcars)

> opar

> par(mfrow=c(2,2))

> plot(wt,mpg,main="Main Title")

> plot(wt,disp,main="Main Tile1")

> hist(wt,main="main tile2")

> boxplot(wt,main="main title3")

> par(opar)

> detach(mtcars)

0818b9ca8b590ca3270a3433284dd417.png

函数layout()的调用形式为layout(mat),其中的mat是一个矩阵,它指定了所要组合的

多个图形的所在位置。在以下代码中,一幅图被置于第1行,另两幅图则被置于第2行:

attach(mtcars)

layout(matrix(c(1,1,2,3),2,2,byrow=T))

hist(wt)

hist(mpg)

hist(disp)

detach(mtcars)

精确地控制每幅图形的大小,可以有选择地在layout()函数中使用widths=和

heights=两个参数。其形式为:

widths = 各列宽度值组成的一个向量

heights = 各行高度值组成的一个向量

相对宽度可以直接通过数值指定,绝对宽度(以厘米为单位)可以通过函数lcm()来指定。

detach(mtcars)

> attach(mtcars)

> layout(matrix(c(1,1,2,3)2,2,byrow=T),widths=c(3,1),heights=c(1,2))

错误: 意外的数值量在"layout(matrix(c(1,1,2,3)2"里

> layout(matrix(c(1,1,2,3),2,2,byrow=T),widths=c(3,1),heights=c(1,2))

> hist(wt)

错误于plot.new() : 图版边太大

> par(mfrow=c(1,1))

> layout(matrix(c(1,1,2,3),2,2,byrow=T),widths=c(3,1),heights=c(1,2))

> hist(wt)

错误于plot.new() : 图版边太大

> layout(matrix(c(1,1,2,3),2,2,byrow=T),widths=c(3,1),heights=c(1,2))

> hist(wt)

错误于plot.new() : 图版边太大

> par(mar=c(5,4,4,2)+0.1)

> layout(matrix(c(1,1,2,3),2,2,byrow=T),widths=c(3,1),heights=c(1,2))

> hist(wt)

错误于plot.new() : 图版边太大

> hist(wt)

错误于plot.new() : 图版边太大

> par(mar=c(5,4,4,2)+0.1)

> hist(wt)

错误于plot.new() : 图版边太大

> par(mar=c(2,2,2,2)+0.1)

> hist(wt)

> hist(mpg)

> hist(disp)

> detach()

0818b9ca8b590ca3270a3433284dd417.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值