如何自定义 shapviz 图?

问题描述 投票:0回答:1

我是 {shapviz} 软件包的最新用户,我在自定义图表(例如重要性图表)时遇到了一些困难。

我想要,例如:

  • 更改蜂群图的调色板和条形图的颜色;
  • 更改图例(标题和大小)[类似
    theme(legend.key.size = unit(2, 'cm')
    ];
  • 更改数字的文本/标签 (
    show_numbers = TRUE
    ) [就像我使用 geom_text( )、
    geom_label( )
    annotation( )
    函数
    ]。

由于 shapviz 图是 ggplot 类型,我尝试通过访问列表或添加图层来更改它们,但没有成功。

谢谢!

r ggplot2 customization shap beeswarm
1个回答
0
投票

使用一些玩具数据,您可以通过以下方式改变这些美学:

library(xgboost)
library(shapviz)
library(viridis)
library(ggplot2)

X_train <- data.matrix(iris[, -1])
dtrain <- xgb.DMatrix(X_train, label = iris[, 1], nthread = 1)
fit <- xgb.train(data = dtrain, nrounds = 10, nthread = 1)
x <- shapviz(fit, X_pred = X_train)

p <- sv_importance(x, kind = "both", show_numbers = TRUE) +
  scale_colour_gradient(low = "darkblue", high = "skyblue", 
                        breaks = c(0, 1), labels = c("low", "high")) +
  labs(colour = "My Title") +
  theme(legend.key.size = unit(2, 'cm'),
        legend.title.position = "left",
        legend.title = element_text(angle = 90, hjust = 0.5)
        )

p$layers[[1]]$aes_params$fill <- "red"
p$layers[[4]]$aes_params$fontface <- "italic"

p

创建于 2024-04-08,使用 reprex v2.1.0

© www.soinside.com 2019 - 2024. All rights reserved.