在R中的同一页面上绘制多个条件直方图

时间:2014-02-05 20:36:07

标签: r plot histogram lattice

所以我试图在一页中绘制多个条件直方图。我有三个数字预测器,AFE,ZCR和ACC,它们需要以0或1的等级为条件。

当我为单个变量执行此操作时:

histogram(myData.toplot$Class ~ myData.toplot$AFE | myData.toplot$Class)

工作正常。现在我想将它们中的所有三个放在同一个图中。我试过这个:

x = histogram(myData.toplot$Class ~ myData.toplot$AFE | myData.toplot$Class)
y = histogram(myData.toplot$Class ~ myData.toplot$ZCR | myData.toplot$Class)
z = histogram(myData.toplot$Class ~ myData.toplot$ACC | myData.toplot$Class)
print(x, position=c(0, 0, 1, .35), more=TRUE)
print(y, position=c(0, .3, 1, .65), more=TRUE)
print(z, position=c(0, .6, 1, .95))

这有效,但是,我不希望必须指定确切的位置c,因为预测变量的数量增加了。所以相反,我尝试了布局功能:

histogram( myData.toplot$Class ~ myData.toplot$AFE +myData.toplot$ZCR +myData.toplot$ACC | myData.toplot$Class ,layout=c(1,3),data = myData.toplot)

但是,这非常令人不满意。它不知道如何正确地对类进行条件化。如何使用网格包在一页中绘制三个条件直方图面板?

还有另一种方法可以做直方图:

set.seed(42)
p1 <- hist(rnorm(500,4))                     # centered at 4
p2 <- hist(rnorm(500,6))                     # centered at 6
plot( p1, col=rgb(0,0,1,1/4), xlim=c(0,10))  # first histogram
plot( p2, col=rgb(1,0,0,1/4), xlim=c(0,10), add=T)  # second

但是当我添加:

par(mfrow=c(3,1))  ##this is what I added
set.seed(42)
p1 <- hist(rnorm(500,4))                     # centered at 4
p2 <- hist(rnorm(500,6))                     # centered at 6
plot( p1, col=rgb(0,0,1,1/4), xlim=c(0,10))  # first histogram
plot( p2, col=rgb(1,0,0,1/4), xlim=c(0,10), add=T)  # second

set.seed(41)
p11 <- hist(rnorm(500,4))                     # centered at 4
p21 <- hist(rnorm(500,6))                     # centered at 6
plot( p11, col=rgb(0,0,1,1/4), xlim=c(0,10))  # first histogram
plot( p21, col=rgb(1,0,0,1/4), xlim=c(0,10), add=T)  # second

set.seed(40)
p12 <- hist(rnorm(500,4))                     # centered at 4
p22 <- hist(rnorm(500,6))                     # centered at 6
plot( p12, col=rgb(0,0,1,1/4), xlim=c(0,10))  # first histogram
plot( p22, col=rgb(1,0,0,1/4), xlim=c(0,10), add=T)  # second

我得到两个页面,一个是p1,p2,p1和p2的组合,另一个页面是p11,p12,p11和p12的组合......等我想使用par()命令简单地将组合分成三行。我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

问题是由于hist创建了一个情节。使用参数plot = FALSE,它将起作用。

par(mfrow=c(3,1))  ##this is what I added
set.seed(42)
p1 <- hist(rnorm(500,4), plot = FALSE)                     # centered at 4
p2 <- hist(rnorm(500,6), plot = FALSE)                     # centered at 6
plot( p1, col=rgb(0,0,1,1/4), xlim=c(0,10))  # first histogram
plot( p2, col=rgb(1,0,0,1/4), xlim=c(0,10), add=T)  # second

set.seed(41)
p11 <- hist(rnorm(500,4), plot = FALSE)                     # centered at 4
p21 <- hist(rnorm(500,6), plot = FALSE)                     # centered at 6
plot( p11, col=rgb(0,0,1,1/4), xlim=c(0,10))  # first histogram
plot( p21, col=rgb(1,0,0,1/4), xlim=c(0,10), add=T)  # second

set.seed(40)
p12 <- hist(rnorm(500,4), plot = FALSE)                     # centered at 4
p22 <- hist(rnorm(500,6), plot = FALSE)                     # centered at 6
plot( p12, col=rgb(0,0,1,1/4), xlim=c(0,10))  # first histogram
plot( p22, col=rgb(1,0,0,1/4), xlim=c(0,10), add=T)  # second

enter image description here