简体   繁体   English

如何自定义软件包“ randomForest”生成的重要性图

[英]How to customize the Importance Plot generated by package “randomForest”

Importance plot: 重要图:

在此处输入图片说明

I want align the y-axis text to right, and also want to color the variables according to different variable group. 我想将y轴文本向右对齐,也想根据不同的变量组为变量着色。 For example Limonene and Valencane, a-Selinene and g-Selinen are in the same group,respecitvelly. 例如柠檬烯和戊烯,a-Selinene和g-Selinen分别在同一组中。

But I can not find any code for customization of plot in the package "randomForest" . 但是我在包“ randomForest”中找不到用于自定义绘图的任何代码。 Do you have a suggestion for the customization? 您对定制有何建议? Thank you! 谢谢!

Here a working example to follow: 以下是一个工作示例:

You need to create the grouping that you want, then use ggplot with geom_bar . 您需要创建所需的分组,然后将ggplotgeom_bar ggplot使用。

set.seed(4543)
data(mtcars)

library(randomForest)
mtcars.rf <- randomForest(mpg ~ ., data=mtcars, ntree=1000, keep.forest=FALSE,
                          importance=TRUE)
imp <- varImpPlot(mtcars.rf) # let's save the varImp object

# this part just creates the data.frame for the plot part
library(dplyr)
imp <- as.data.frame(imp)
imp$varnames <- rownames(imp) # row names to column
rownames(imp) <- NULL  
imp$var_categ <- rep(1:2, 5) # random var category

# this is the plot part, be sure to use reorder with the correct measure name
library(ggplot2) 
ggplot(imp, aes(x=reorder(varnames, IncNodePurity), weight=IncNodePurity, fill=as.factor(var_categ))) + 
  geom_bar() +
  scale_fill_discrete(name="Variable Group") +
  ylab("IncNodePurity") +
  xlab("Variable Name")

You can do the same for the other importance measure, just change the plot part accordingly ( weight = %IncMSE ). 您可以对其他重要性度量执行相同的操作,只需相应地更改绘图部分( weight = %IncMSE )。

在此处输入图片说明

Update based on OP answer: 根据OP答案进行更新:

ggplot(imp, aes(x=reorder(varnames, IncNodePurity), y=IncNodePurity, color=as.factor(var_categ))) + 
  geom_point() +
  geom_segment(aes(x=varnames,xend=varnames,y=0,yend=IncNodePurity)) +
  scale_color_discrete(name="Variable Group") +
  ylab("IncNodePurity") +
  xlab("Variable Name") +
  coord_flip()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM