1.par()
- par(mfrow=c(nrows, ncols))或par(nfcol=c(nrows, ncols))
举例来说,以下代码创建了四幅图形并将其排布在两行两列中:
- setwd("D:/STUDY/Routput") # 设置工作目录
- bmp(file="combininggraphs.bmp")
- attach(mtcars)
- opar <- par(no.readonly = TRUE)
- par(mfrow = 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")
- par(opar)
- detach(mtcars)
- dev.off()
作为第二个示例,让我们依3行1列排布3幅图形。代码如下:
- setwd("D:/STUDY/Routput") # 设置工作目录
- bmp(file="fig3.15.bmp")
- attach(mtcars)
- opar <- par(no.readonly = TRUE)
- par(mfrow = c(3, 1))
- hist(wt)
- hist(mpg)
- hist(disp)
- par(opar)
- detach(mtcars)
- dev.off()
2.函数layout()
layout(mat, widths = rep.int(1, ncol(mat)),heights = rep.int(1, nrow(mat)), respect = FALSE)
函数layout()的调用形式为layout(mat),其中的mat是一个矩阵,它指定了所要组合的多个图形的所在位置。在以下代码中,一幅图被置于第1行,另两幅图则被置于第2行:
- setwd("D:/STUDY/Routput") # 设置工作目录
- bmp(file="fig3.16.bmp")
- attach(mtcars)
- layout(matrix(c(1, 1, 2, 3), 2, 2, byrow = TRUE))
- hist(wt)
- hist(mpg)
- hist(disp)
- detach(mtcars)
- dev.off()
为了更精确地控制每幅图形的大小,可以有选择地在layout()函数中使用widths=和heights=两个参数。其形式为:
widths = 各列宽度值组成的一个向量
heights = 各行高度值组成的一个向量
- # Figure 3.17 更精确地控制每幅图形的大小
- setwd("D:/STUDY/Routput") # 设置工作目录
- bmp(file="fig3.17.bmp")
- attach(mtcars)
- layout(matrix(c(1, 1, 2, 3), 2, 2, byrow = TRUE),
- widths = c(3, 1), heights = c(1, 2))
- hist(wt)
- hist(mpg)
- hist(disp)
- detach(mtcars)
- dev.off()
如果想要了解layout()更多信息,请查询help(layout)
3.fig=
- par(fig=c(x1,x2,y1,y2),new=TRUE)
多幅图形布局的精细控制-图形参数fig=以下代码通过在散点图上添加两幅箱线图,创建了单幅的增强型图形
- # 多幅图形布局的精细控制-图形参数fig=
- setwd("D:/STUDY/Routput") # 设置工作目录
- bmp(file="fig3.18.bmp")
- opar <- par(no.readonly = TRUE)
- par(fig = c(0, 0.8, 0, 0.8))
- plot(mtcars$wt, mtcars$mpg, xlab = "Miles Per Gallon",
- ylab = "Car Weight")
- par(fig = c(0, 0.8, 0.55, 1), new = TRUE)
- boxplot(mtcars$wt, horizontal = TRUE, axes = FALSE)
- par(fig = c(0.65, 1, 0, 0.8), new = TRUE)
- boxplot(mtcars$mpg, axes = FALSE)
- mtext("Enhanced Scatterplot", side = 3, outer = TRUE,
- line = -3)
- par(opar)
- dev.off()
- # 你需要不断尝试找到合适的位置参数
小白学习R,希望能帮上大家