2012-11-01 23 views
2

我想知道为什么下面这段代码不会产生一个在另一个之下的两个图。par(mfcol = c(2,1))和第二行空白

data(mtcars) 
library(randomForest) 

mtcars.rf <- randomForest(mpg ~ ., data=mtcars, ntree=1000, keep.forest=FALSE, 
          importance=TRUE) 
png("rf1.png", width=6, height=6, units="in", res=100) 
par(mfcol=c(2,1)) 
varImpPlot(mtcars.rf) 
plot(mtcars.rf, log="y") 
dev.off() 

这只是产生

enter image description here

一个空白第二行。

回答

3

问题是varImpPlot重新定义了绘图区域,然后将其重置为先前的值。这意味着它的行为就好像你在行varImpPlot行后调用par(mfcol=c(2,1))。如果提取varImpPlot数据绘制你可以自己绘制两个dotcharts(你可以使用layout而不是par分裂绘图区域分成不同的定形区域):

data(mtcars) 
library(randomForest) 

mtcars.rf <- randomForest(mpg ~ ., data=mtcars, ntree=1000, keep.forest=FALSE, 
          importance=TRUE) 
varImpData <- varImpPlot(mtcars.rf) # calculate this outside the plot 

png("rf1.png", width=6, height=6, units="in", res=100) 
layout(matrix(c(1,2,3,3), 2, 2, byrow = TRUE)) 
dotchart(varImpData[,c(1)]) 
dotchart(varImpData[,c(2)]) 
plot(mtcars.rf, log="y") 
dev.off()