我正在使用networkD3包中的sankeyNetwork来创建可视化
我想为每个边分配一个名称/ ID,以便显示在工具提示中。可以使用sankeyNetwork或networkD3包中的任何其他功能来完成吗?
答案 0 :(得分:0)
这在技术上不受支持,但你可以像这样实现......
library(networkD3)
library(htmlwidgets)
links <- data.frame(
src = c(0, 0, 1, 2),
target = c(2, 3, 2, 4),
value = 1,
name = c("first", "second", "third", "fourth")
)
nodes <- data.frame(name = c("one", "two", "three", "four", "five"))
# save the result of sankeyNetwork in an object
sn <- networkD3::sankeyNetwork(
Links = links,
Nodes = nodes,
Source = 'src',
Target = 'target',
Value = 'value',
NodeID = 'name'
)
# add the names back into the links data because sankeyNetwork strips it out
sn$x$links$name <- links$name
# add onRender JavaScript to set the title to the value of 'name' for each link
sn <- htmlwidgets::onRender(
sn,
'
function(el, x) {
d3.selectAll(".link").select("title foreignObject body pre")
.text(function(d) { return d.name; });
}
'
)
# display the result
sn