I've am using sankeyNetwork in the networkD3 package to create a visualisation
I would like to assign a name/ID to each edge, so that is appears in the tooltip. Can this be done with sankeyNetwork or any other function in the networkD3 package?
I've am using sankeyNetwork in the networkD3 package to create a visualisation
I would like to assign a name/ID to each edge, so that is appears in the tooltip. Can this be done with sankeyNetwork or any other function in the networkD3 package?
This is not technically supported, but you can achieve it like this...
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