Home > front end >  How to customize x-axis range with shapviz
How to customize x-axis range with shapviz

Time:12-12

I'm plotting the shap values of my variables using the shapviz package. Specifically, I'm plotting a beeswarm plot using the sv_importance command, and dependence plots for the most important variables using sv_dependence. However, to make the results of different models more easily comparable, I would like to customize the x-axis range to make it equal for every plot. Do you have any suggestions about how to customize the axis range for shapviz objects? Here is a reproducible example:

library(shapviz)

set.seed(1)

X_train <- data.matrix(`colnames<-`(replicate(26, rnorm(100)), LETTERS))
dtrain <- xgboost::xgb.DMatrix(X_train, label = rnorm(100))
fit <- xgboost::xgb.train(data = dtrain, nrounds = 50)
shp <- shapviz(fit, X_pred = X_train)

p <- sv_importance(shp, kind = "beeswarm", show_numbers = TRUE, max_display = 15) 

p

d <- sv_dependence(shp, v="I") 
d

From here, how can I make the x-axis range in the plot p equal to [-2.0, 2.0] instead of [-1.0, 0.5] (as it is by default)?

CodePudding user response:

p is a ggplot object, so you can add whatever scale you like. Just be aware that the "x" axis is actually the y axis, since coord_flip is used internally:

library(ggplot2)

p   scale_y_continuous(limits = c(-2, 2))

enter image description here

  • Related