更改 H2O 函数返回的 ggplot object 中的几何颜色
Change the geom color in ggplot object returned by H2O function
H2O 包提供了绘制每个观测值的函数。请参阅以下取自 r 文档的代码。
函数 returns a ggplot object 但我无法更改条形图的颜色。我可以添加标题、注释等,但无法访问 geom。我想将正值着色为粉红色,将负值着色为蓝色
library(h2o)
h2o.init()
# Import the wine dataset into H2O:
f <- "https://h2o-public-test-data.s3.amazonaws.com/smalldata/wine/winequality-redwhite-no-BOM.csv"
df <- h2o.importFile(f)
# Set the response
response <- "quality"
# Split the dataset into a train and test set:
splits <- h2o.splitFrame(df, ratios = 0.8, seed = 1)
train <- splits[[1]]
test <- splits[[2]]
# Build and train the model:
gbm <- h2o.gbm(y = response,
training_frame = train)
# Create the SHAP row explanation plot
shap_explain_row_plot <- h2o.shap_explain_row_plot(gbm, test, row_index = 1)
print(shap_explain_row_plot)
这可以像这样实现。
首先,检查 ggplot 对象,我们看到 geom_col
的填充颜色设置为参数。
library(ggplot2)
gg <- shap_explain_row_plot
gg$layers[[1]]
#> geom_col: width = NULL, na.rm = FALSE, fill = #b3ddf2, flipped_aes = FALSE
#> stat_identity: na.rm = FALSE
#> position_stack
因此,为了映射 fill
美学,我们首先必须通过
删除填充参数
gg$layers[[1]]$aes_params$fill <- NULL
其次,从 mapping
我们看到,条形的长度对应于映射到 y
审美的变量 contribution
。
gg$mapping
#> Aesthetic mapping:
#> * `x` -> `.data$feature`
#> * `y` -> `.data$contribution`
#> * `text` -> `.data$text`
因此,为了获得您想要的结果,您可以在填充美学上映射 contribution < 0
并通过 scale_fill_manual
设置所需的颜色值
gg + aes(fill = contribution < 0) + scale_fill_manual(values = c("TRUE" = "blue", "FALSE" = "pink"))
由 reprex package (v1.0.0)
于 2021-04-01 创建
H2O 包提供了绘制每个观测值的函数。请参阅以下取自 r 文档的代码。
函数 returns a ggplot object 但我无法更改条形图的颜色。我可以添加标题、注释等,但无法访问 geom。我想将正值着色为粉红色,将负值着色为蓝色
library(h2o)
h2o.init()
# Import the wine dataset into H2O:
f <- "https://h2o-public-test-data.s3.amazonaws.com/smalldata/wine/winequality-redwhite-no-BOM.csv"
df <- h2o.importFile(f)
# Set the response
response <- "quality"
# Split the dataset into a train and test set:
splits <- h2o.splitFrame(df, ratios = 0.8, seed = 1)
train <- splits[[1]]
test <- splits[[2]]
# Build and train the model:
gbm <- h2o.gbm(y = response,
training_frame = train)
# Create the SHAP row explanation plot
shap_explain_row_plot <- h2o.shap_explain_row_plot(gbm, test, row_index = 1)
print(shap_explain_row_plot)
这可以像这样实现。
首先,检查 ggplot 对象,我们看到 geom_col
的填充颜色设置为参数。
library(ggplot2)
gg <- shap_explain_row_plot
gg$layers[[1]]
#> geom_col: width = NULL, na.rm = FALSE, fill = #b3ddf2, flipped_aes = FALSE
#> stat_identity: na.rm = FALSE
#> position_stack
因此,为了映射 fill
美学,我们首先必须通过
gg$layers[[1]]$aes_params$fill <- NULL
其次,从 mapping
我们看到,条形的长度对应于映射到 y
审美的变量 contribution
。
gg$mapping
#> Aesthetic mapping:
#> * `x` -> `.data$feature`
#> * `y` -> `.data$contribution`
#> * `text` -> `.data$text`
因此,为了获得您想要的结果,您可以在填充美学上映射 contribution < 0
并通过 scale_fill_manual
gg + aes(fill = contribution < 0) + scale_fill_manual(values = c("TRUE" = "blue", "FALSE" = "pink"))
由 reprex package (v1.0.0)
于 2021-04-01 创建