Displaying edge information in Sankey tooltip

喜欢而已 提交于 2020-01-11 07:17:08

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/45635970/displaying-edge-information-in-sankey-tooltip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!