我目前正在尝试可视化我的变量重要性图,但我的图看起来都被压扁了。我对 R 不太熟悉,所以我想问是否有人知道如何使绘图更具可读性/更好看,或者如何完全删除
MeanDecreaseGini
部分?
model <- randomForest(Y ~ ., data = data, ntree = 500, importance = TRUE, proximity = TRUE)
varImpPlot(model, n.var = 15)
我尝试使用
randomForest::varImpPlot(model,
sort=FALSE,
main="Variable Importance Plot")
但这并没有帮助。
使用玩具示例,一个选项可能是将
varImpPlot
输出(第一个图)保存为数据框,然后使用具有高度定制灵活性的 ggplot2
(第二个图)。
library(randomForest)
library(tidyverse)
library(janitor)
set.seed(4543)
data(mtcars)
rf <- randomForest(mpg ~ .,
data = mtcars, ntree = 1000,
importance = TRUE,
proximity = TRUE
)
vi <- varImpPlot(rf)
vi |>
as_tibble(rownames = "variable") |>
clean_names() |>
pivot_longer(-variable) |>
ggplot(aes(value, variable)) +
geom_col() +
facet_wrap(~ name, scales = "free_x") +
theme_bw() +
labs(x = NULL, y = NULL)
创建于 2024-04-14,使用 reprex v2.1.0