Shapviz beeswarm plot color options

580 Views Asked by At

I'm plotting a beeswarm plot for shap values in R with shapviz command. I'm using the following code:

sv_importance(shp1, kind= "beeswarm")

However, I would like to increase the transparency of the dots in the plot. Do you have any suggestions about how to do it? Thanks!

2

There are 2 best solutions below

1
On BEST ANSWER

It is easy: just pass alpha to sv_importance(), like this:

sv_importance(shp1, kind= "beeswarm", alpha = 0.2)

You can also set dot size or beeswarm width etc.

Disclaimer: I am the author of "shapviz"

0
On

We don't have your data, so I created a reproducible example. Because shapviz has ggplot objects, you can use ggplot_build to change the alpha (for transparency) to whatever you want. Here I give an example of transparency of 0.5:

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

set.seed(3653)
diamonds <- diamonds[sample(nrow(diamonds), 100),]
X <- diamonds[c("carat", "cut", "color", "clarity")]
dtrain <- xgb.DMatrix(data.matrix(X), label = diamonds$price)

fit <- xgb.train(
  params = list(learning_rate = 0.1, objective = "reg:squarederror"), 
  data = dtrain,
  nrounds = 65L
)

X_small <- X[sample(nrow(X), 20L), ]

shp1 <- shapviz(fit, X_pred = data.matrix(X_small), X = X_small)

p <- sv_importance(shp1, kind= "beeswarm")
p

# Here starts important code
q <- ggplot_build(p)

# Change transpacreny
q$data[[2]]$alpha <- 0.5

q <- ggplot_gtable(q)
plot(q)

Created on 2022-08-17 with reprex v2.0.2

As you can see, the transparency has been changed.