Jessica Beyer
Jessica Beyer

Reputation: 158

How to set x limits on varImpPlot

How can I change the x limits of a plot produced by varImpPlot from the randomForest package?

If I try

set.seed(4543)
data(mtcars)
mtcars.rf <- randomForest(mpg ~ ., data=mtcars, ntree=1000, keep.forest=FALSE,
                      importance=TRUE)
varImpPlot(mtcars.rf, scale=FALSE, type=1, xlim=c(0,15))

I get the following error:

Error in dotchart(imp[ord, i], xlab = colnames(imp)[i], ylab = "", main = if (nmeas == : formal argument "xlim" matched by multiple actual arguments".

This is because varImpPlot defines its own x limits, I think, but how could I get around this if I wanted to set the x limits myself (perhaps for consistency across plots)?

Upvotes: 2

Views: 2535

Answers (1)

Jessica Beyer
Jessica Beyer

Reputation: 158

First I extracted the values using importance() (thanks to the suggestion from @dww)

impToPlot <- importance(mtcars.rf, scale=FALSE)

Then I plotted them using dotchart(), which allowed me to manually set the x limits (and any other plot features I'd like)

dotchart(sort(impToPlot[,1]), xlim=c(0,15), xlab="%IncMSE")

dotchart of variable importance with manual x axis

Upvotes: 5

Related Questions