r - 绘制由 Ranger 函数计算的特征重要性

标签 r ggplot2

我需要使用 绘制变量重要性游侠函数,因为我有一个大数据表和 随机森林 在我的研究案例中不起作用。

这是我的代码:

library(ranger)
set.seed(42)
model_rf <- ranger(Sales ~ .,data = data[,-1],importance = "impurity")

然后我创建新的数据框 东风 其中包含来自上面的代码,如下所示
> v<-as.vector(model_rf$variable.importance$Importance)
> w<-(as.vector((row.names(df))))
> DF<-cbind(w,v)
> DF<-as.data.frame(DF)
> DF
                           w                v
1                  DayOfWeek 376393213095.426
2                  Customers 1364058809531.96
3                       Open 634528877741.021
4                      Promo 261749509069.205
5               StateHoliday 5196666310.34041
6              SchoolHoliday  6522969049.3763
7                   DateYear  7035399071.0376
8                  DateMonth 20134820116.2625
9                    DateDay 37631766745.2306
10                  DateWeek 32834962167.9479
11                 StoreType 31568433413.5718
12                Assortment 20257406597.8358
13       CompetitionDistance  111847579772.77
14 CompetitionOpenSinceMonth 46332196019.0118
15  CompetitionOpenSinceYear 45548903472.6485
16                    Promo2                0
17           Promo2SinceWeek 50666744628.7906
18           Promo2SinceYear 40964066303.0407
19           CompetitionOpen 39927447341.0351
20                 PromoOpen  28319356095.063
21            IspromoinSales 2844220121.08598

但是我需要根据上面显示的结果绘制这样的图形:

enter image description here

编辑

正如@Sam 提议的那样,我尝试修改此代码:
> ggplot(DF, aes(x=reorder(w,v), y=v,fill=v))+ 
+   geom_bar(stat="identity", position="dodge")+ coord_flip()+
+   ylab("Variable Importance")+
+   xlab("")+
+   ggtitle("Information Value Summary")+
+   guides(fill=F)+
+   scale_fill_gradient(low="red", high="blue")

但我收到此错误:

Error: Discrete value supplied to continuous scale In addition: There were 42 warnings (use warnings() to see them) >



请问我该怎么做?
先感谢您!

最佳答案

这是未经测试的,但我认为这应该给你你所追求的:

 ggplot(model_rf$variable.importance, aes(x=reorder(variable,importance), y=importance,fill=importance))+ 
      geom_bar(stat="identity", position="dodge")+ coord_flip()+
      ylab("Variable Importance")+
      xlab("")+
      ggtitle("Information Value Summary")+
      guides(fill=F)+
      scale_fill_gradient(low="red", high="blue")

关于r - 绘制由 Ranger 函数计算的特征重要性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49105358/

相关文章:

r - 查找字符串序列在其他向量中的位置

r - 对依赖于先前元素的乘积计算进行矢量化?

r - 在 R 中添加对象(如 ggplot 层)

r - 为绘图排序字母数字变量

r - ggplot2 PDF 输出中的 Unicode 字符

r - 如何隐藏或禁用pickerInput中的一项选择多项

r - 如何通过变量将参数传递到 data.table[J()]

r - 嵌套 ifelse 语句中未使用的参数错误

r - ggplot2 热图 : using different gradients for categories

r - ggplot 带虚线的箭头