如何在R中的一个面板中显示多个图?

时间:2014-02-12 04:23:32

标签: r plot histogram

我有以下情节,我想在一个面板中显示所有这些情节!我怎么能用矩阵呢?另外我想知道是否有其他方法,而不是使用matrixlayout

> plot(density(Boston$tax))
> rug(Boston$tax, col=2, lwd=3.5)
> hist(Boston$tax)
> rug(Boston$tax, col=2, lwd=3.5)
> table(Boston$chas)
Off  On 
471  35 
> barplot(table(Boston$chas))

> f1<-layout(matrix(c(0, 1,1,1,0,2,2,2,0,3,3,3) ,nrow = 4, ncol = 4, byrow = TRUE))
> layout.show(f1)

我希望在我的情节1,2和3中有这样的结构:

##      [,1] [,2] [,3] [,4]
## [1,]    0    1    1   1
## [2,]    0    2    2   2
## [3,]    0    3    3   3
## [4,]    blank0    0   0   

然而,我的代码输出显示了不同的东西: enter image description here 有人可以向我解释下面的图c(...)是如何构建的? enter image description here

1 个答案:

答案 0 :(得分:5)

来自?layout

Description:

     ‘layout’ divides the device up into as many rows and columns as
     there are in matrix ‘mat’, with the column-widths and the
     row-heights specified in the respective arguments.

所以如果我们的矩阵是

matrix(1:4, 2, 2, byrow = TRUE)
##      [,1] [,2]
## [1,]    1    2
## [2,]    3    4 
我们的布局是这样的

enter image description here

如果我们只想在顶行上绘制1个图,我们可以将矩阵指定为

matrix(c(1, 1, 2, 3), 2, 2, byrow = TRUE)
##      [,1] [,2]
## [1,]    1    1
## [2,]    2    3

,布局将是

enter image description here

mat <- matrix(1:3, 3, 3)
mat <- rbind(cbind(0, mat), 0)
##      [,1] [,2] [,3] [,4]
## [1,]    0    1    1    1
## [2,]    0    2    2    2
## [3,]    0    3    3    3
## [4,]    0    0    0    0

enter image description here

layout(mat)

plot(density(Boston$tax))
rug(Boston$tax, col=2, lwd=3.5)
hist(Boston$tax)
rug(Boston$tax, col=2, lwd=3.5)
barplot(table(Boston$chas))

enter image description here