2012-11-01 26 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()