我想创建一个与下面的瀑布图类似的瀑布图,并在瀑布图段之间使用连接条,并且根据以下内容看来是可能的。
#would like a plot similar to this with connecting bars
library(waterfalls)
library(plotly)
waterfall(.data = data.frame(category = letters[1:5],
value = c(100, -20, 10, 20, 110)),
calc_total = T,
fill_by_sign = T)
#appears to be possible
p <- waterfall(.data = data.frame(category = letters[1:5],
value = c(100, -20, 10, 20, 110)),
calc_total = T,
fill_by_sign = T)
ggplotly(p)
看图,似乎字母标签实际上是数字(?)。虽然我不太确定如何创建带有数字索引的条形图。以下是我尝试使其正确的尝试。有人可以在这里向我指出正确的方向吗?
library(waterfalls)
library(plotly)
shap <- structure(list(values = c(5.82983875274658, 0, 0, 0, -0.0259701404720545, -0.103678397834301, -1.02624976634979, 0),
names = structure(1:8, .Label = c("bias", "speciessetosa", "speciesversicolor", "speciesvirginica", "sepal_width", "petal_width", "petal_length", "Ttl. Target Pred"), class = c("ordered", "factor")),
base = c(0, 5.82983875274658, 5.82983875274658, 5.82983875274658, 5.82983875274658, 5.80386861227453, 5.70019021444023, 0),
positive = c(5.82983875274658, 0, 0, 0, 0, 0, 0, 0),
negative = c(0, 0, 0, 0, -0.0259701404720545, -0.103678397834301, -1.02624976634979, 0),
shap_total = c(0, 0, 0, 0, 0, 0, 0, 4.67394044809043),
position = c(2.91491937637329, 5.82983875274658, 5.82983875274658, 5.82983875274658, 5.81685368251055, 5.75202941335738, 5.18706533126533, 2.33697022404522),
text_vals = c("5.83", "0", "0", "0", "-0.03", "-0.1", "-1.03", "4.67"), row_num = 1:8),
class = "data.frame", row.names = c(NA, -8L))
p <- plotly::plot_ly(shap, y = ~names, x = ~base, type = 'bar', marker = list(color = 'rgba(1,1,1, 0.0)')) %>%
add_trace(x = ~positive, marker = list(color = 'rgba(50, 171, 96, 0.7)',
line = list(color = 'rgba(50, 171, 96, 1.0)',
width = 2))) %>%
add_trace(x = ~negative, marker = list(color = 'rgba(219, 64, 82, 0.7)',
line = list(color = 'rgba(219, 64, 82, 1.0)',
width = 2))) %>%
add_trace(x = ~shap_total, marker = list(color = 'rgba(55, 128, 191, 0.7)',
line = list(color = 'rgba(55, 128, 191, 1.0)',
width = 2))) %>%
layout(title = 'SHAP Value Prediction Contributions',
xaxis = list(title = "Prediction Contribution"),
yaxis = list(title = ""),
barmode = 'stack',
showlegend = FALSE) %>%
add_annotations(text = ~text_vals,
y = ~names,
x = ~position,
xref = "x",
yref = "y",
font = list(family = 'Arial',
size = 12,
color = 'rgba(0, 0, 0, 1)'),
showarrow = FALSE)
p