本文介绍了在Sankey工具提示中显示边缘信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用networkD3软件包中的sankeyNetwork来创建可视化效果

I've am using sankeyNetwork in the networkD3 package to create a visualisation

我想为每个边缘分配一个名称/ID,以便出现在工具提示中.可以使用sankeyNetwork或networkD3软件包中的任何其他功能来完成此操作吗?

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

这篇关于在Sankey工具提示中显示边缘信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 16:40