[英]Legend for Random Forest Plot in R
我使用randomForest函數在R中創建了一個隨機森林預測模型:
model = randomForest(classification ~., data=train, ntree=100, proximity=T)
接下來,我繪制模型以查看模型的整體錯誤:
plot(model, log="y")
這給了我以下情節:
我的問題是如何在這上面添加一個圖例,以便我可以看到哪個顏色對應於用於分類的因子中的每個值? 因子變量是data$classification
。 我無法弄清楚要做到這一點的legend()調用。
圖S3方法圖使用matplot
繪制隨機森林模型。 您應該手動添加圖例。 這應該是一個好的開始:
library(randomForest)
model = randomForest(Species ~., data=iris, ntree=100, proximity=T)
layout(matrix(c(1,2),nrow=1),
width=c(4,1))
par(mar=c(5,4,4,0)) #No margin on the right side
plot(model, log="y")
par(mar=c(5,0,4,2)) #No margin on the left side
plot(c(0,1),type="n", axes=F, xlab="", ylab="")
legend("top", colnames(model$err.rate),col=1:4,cex=0.8,fill=1:4)
你可以用這個,
model$finalModel.legend <- if (is.null(model$finalModel$test$err.rate)) {colnames(model$finalModel$err.rate)} else {colnames(model$finalModel$test$err.rate)}
legend("top", cex =0.7, legend=model$finalModel.legend, lty=c(1,2,3), col=c(1,2,3), horiz=T)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.