1.函数简介

layout(mat, widths = rep.int(1, ncol(mat)),
       heights = rep.int(1, nrow(mat)), respect = FALSE)
layout.show(n)

2. 示例

(1) 画板分为6块

layout(matrix(1:6, nrow=3, byrow = T) )
layout.show(6)

(2) 分为3块,控制高度比例1:2,序号相同的合并为一块

par(oma = c(2,2,2,2))
mat=matrix(c(1, 2, 1, 3), 2); mat
##      [,1] [,2]
## [1,]    1    1
## [2,]    2    3
layout(mat, widths = c(1, 1), heights = c(1, 2))
layout.show(3)

(3) 每一块对应一个图

mat=matrix(c(1, 2, 1, 3), 2); mat
##      [,1] [,2]
## [1,]    1    1
## [2,]    2    3
layout(mat, widths = c(1, 1), heights = c(1, 2))
par(mar=c(2, 3, 1.5, 0.5)) #margin: c(bottom, left, top, right)
plot(mtcars$mpg, mtcars$disp, col="red", pch=19, main="fig1")
hist(iris$Sepal.Length, col="orange", main="fig2")
boxplot(mtcars$mpg, col="purple", main="fig3")

绘制QQ群logo图

实例: plot an logo for R QQ群(187923577)

# layout() 布局函数

#pdf("logoR.pdf", width=2.5, height=2.5)
#png("logoR.png", width=2.5, height=2.5, units = "in", res = 72)
#settings
border_width=30
border_color=c("#71BE4B","#ED245B","#FAC319", "#00A1DF")
r=0.85 #radius of circle
#
par(oma=c(0.1,0.1,0.1,0.1)*0) #设置外边界
# 设置为4块,2行,按行排。宽度比1:1,高度比1:1
layout(matrix(c(1:4),nrow=2,byrow=T), widths = c(1,1), heights =c(1,1)) 
for(i in 1:4){
  # 分别在4个块种画空白图
  par(mar=c(0,0,0,0))
  plot(c(0), col='white', xlim=c(-0.5,0.5), ylim=c(-0.5, 0.5), 
       xaxs ="i", yaxs ="i", # 图形与坐标轴不留空白,默认是r
       ann=F, # 不要坐标标签和标题
       axes=F, # 不要坐标和边框
       main="");
  # 接下来依赖循环变量 i,控制圆心位置
  #circle 外圆环
  cir_X=-0.5*(-1)^i
  cir_Y=0.5*ifelse(i<=2, -1, 1)
  # 能画6种形状: circles, squares, rectangles, stars, thermometers, and boxplots
  symbols(x = cir_X, y = cir_Y, circles = r, inches = FALSE, 
          add = TRUE, #画在上一层
          lwd=border_width, #线条宽度
          fg=border_color[i]) #线条颜色
  # text R 内文字
  text(cir_X, cir_Y, labels="R", col=rev(border_color)[i], cex=15, xpd=T)
}

#dev.off()