绘图功能重要性由游侠功能计算

Rpr*_*mer 4 r ggplot2

我需要使用游侠功能来绘制变量重要性,因为我有一个大数据表,而randomForest在我的研究案例中不起作用。

这是我的代码:

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

然后创建新的数据框DF,其中包含上述代码

> 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
Run Code Online (Sandbox Code Playgroud)

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

在此处输入图片说明

编辑

正如@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")
Run Code Online (Sandbox Code Playgroud)

但是我得到这个错误:

错误:离散值提供给连续刻度此外:有42条警告(使用警告()查看它们)>

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

Sam*_*Sam 5

这未经测试,但我认为这应该给您带来的好处:

 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")
Run Code Online (Sandbox Code Playgroud)