R 组合图形布局教程

本文介绍组合多个图为整体一个图,使用par() 或 layout函数。

1. par()函数

使用par()函数,可以使用选项mfrow=c(nrows, nclos) 创建按行填充矩阵,mfcol=c(nrows, nclos) 按列填充。

# 1 figures arranged in 2 rows and 2 columns
attach(mtcars)
par(mfcol=c(2,2))
plot(wt,mpg, main="Scatterplot of wt vs. mpg")
plot(wt,disp, main="Scatterplot of wt vs disp")
hist(wt, main="Histogram of wt")
boxplot(wt, main="Boxplot of wt")
detach(mtcars)

在这里插入图片描述

再看一个示例:

# 2 figures arranged in 3 rows and 1 column
attach(mtcars)
par(mfrow=c(3,1))
hist(wt)
hist(mpg)
hist(disp)
detach(mtcars)

在这里插入图片描述

2. layout()函数

上面布局比较简单,layout可以实现复杂布局。首先我们看其参数:

  • mat: 矩阵,其每个元素表示图形的位置
  • widths: 表示列宽度的向量. 也可以使用lcm 函数精确设置
  • heights: 表示列的高度向量,也可以使用lcm 函数精确设置
  • respect: 布尔或矩阵,由与mat相同维度的0和1填充,以表示是否尊重宽度和高度之间的关系。

可以利用layout.show函数预览布局,方便我们理解:

l <- layout(matrix(c(1, 2,  # First, second
                     3, 3), # and third plot
            nrow = 2,
            ncol = 2,
            byrow = TRUE))

layout.show(l)

在这里插入图片描述

下面我们看一个示例,第一行放两个图,第二行放一个图:

mat <- matrix(c(1, 2,  # First, second
                3, 3), # and third plot
              nrow = 2, ncol = 2,
              byrow = TRUE)

layout(mat = mat)

# Data
set.seed(6)
x <- rexp(50)

plot(x)    # First plot
boxplot(x) # Second plot
hist(x)    # Third plot

在这里插入图片描述

下面示例,定义两行,第一行放一个图形,第二行放两个图形,且比第一行高三倍:

# 设置边缘空间
par(mar = c(1, 1, 1, 1))
mat <- matrix(c(1, 1,  # First
                2, 3), # second and third plot
              nrow = 2, ncol = 2,
              byrow = TRUE)

layout(mat = mat,
       heights = c(1, 3)) # First and second row
                          # relative heights

# Data
set.seed(6)
x <- rexp(50)

plot(x)    # First row
boxplot(x) # Second row, left
hist(x)    # Second row, right

在这里插入图片描述

我们再看一个示例,生成两列布局,左边放一个图形,右边放两个图形,且右边列宽度是左边两倍:

mat <- matrix(c(1, 2,  # First, second
                1, 3), # first and third plot
              nrow = 2, ncol = 2,
              byrow = TRUE)

layout(mat = mat,
       widths = c(1, 2)) # First and second
                         # column relative widths

# Data
set.seed(6)
x <- rexp(50)

plot(x)    # First column, top
boxplot(x) # First column, bottom
hist(x)    # Second column

在这里插入图片描述

3. 组合使用

使用layout函数也可以结合边缘控制,需要对每个图参数进行设置:
mar 指定数值向量 c(bottom, left, top, right) ,指定图的四个边缘空间,缺省为 c(5, 4, 4, 2) + 0.1。

# Data
set.seed(6)
x <- rexp(50)

# 这里0表示保留空白,不放置图形

layout(matrix(c(2, 0, 1, 3),
              nrow = 2, ncol = 2,
              byrow = TRUE),
       widths = c(3, 1),
       heights  = c(1, 3), respect = TRUE)

# Top and right margin of the main plot
par(mar = c(5.1, 4.1, 0, 0))
plot(x)

# Left margin of the histogram
par(mar = c(0, 4.1, 0, 0))
hist(x, main = "", bty = "n",
     axes = FALSE, ylab = "")

# Bottom margin of the boxplot
par(mar = c(5.1, 0, 0, 0))

# Boxplot without plot region box
par(bty = "n")

# Boxplot without axes
boxplot(x, axes = FALSE)

在这里插入图片描述

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值