在R中,如何在Sankey Graph的links/paths上显示值?

In R, how to display value on the links/paths of Sankey Graph?

背景

我正在尝试创建如下图所示的 Sankey 图。实际上,我想获得一个输出,其中将在路径(从一个节点到另一个节点)中设置值(10、20、30、40)。

我是怎么试的?

起初,我尝试使用 R 中的 SO 的 Plotly library of Python. However, somewhere I have seen that it is not possible to set the values in the links or the paths of Sankey graph in Plotly (of Python). Later, I switched to R (for some other reasons also) where more resources are available (I think). However, here, I am also facing the same problem. I have checked many tutorials (e.g., this one), Q&A (e.g., 1, 2, 3)。尽管如此,我还是找不到在路径中显示值的教程或资源!

我的问题

如何在 R 中显示桑基图 links/paths 上的值?

注意: and this SO 的问题似乎是相似的。但是,我不明白如何将它们合并到我的代码中。

示例代码(收集自here

# install.packages('networkD3')
library(networkD3)
nodes = data.frame("name" = 
 c("Node A", # Node 0
 "Node B", # Node 1
 "Node C", # Node 2
 "Node D"))# Node 3
links = as.data.frame(matrix(c(
 0, 1, 10, # Each row represents a link. The first number
 0, 2, 20, # represents the node being conntected from. 
 1, 3, 30, # the second number represents the node connected to.
 2, 3, 40),# The third number is the value of the node
 byrow = TRUE, ncol = 3))
names(links) = c("source", "target", "value")


 sankeyNetwork(Links = links, Nodes = nodes,
 Source = "source", Target = "target",
 Value = "value", NodeID = "name",
 fontSize= 50, nodeWidth = 30)

这可以通过在使用 htmlwidgets::onRender() 呈现时注入自定义 JavaScript 代码来实现。下面的示例最初会适当定位 link 标签,但如果手动移动节点,link 标签将不会相应地自动更新。为此,您可能还必须覆盖默认的 dragmove 行为。

library(htmlwidgets)
library(networkD3)

nodes <- 
  data.frame(
    name = c("Node A", "Node B", "Node C", "Node D")
  )

links <- 
  data.frame(
    source = c(0, 0, 1, 2),
    target = c(1, 2, 3, 3),
    value = c(10, 20, 30, 40)
  )

p <- sankeyNetwork(Links = links, Nodes = nodes,
                   Source = "source", Target = "target",
                   Value = "value", NodeID = "name",
                   fontSize= 20, nodeWidth = 30)

htmlwidgets::onRender(p, '
  function(el) { 
    var nodeWidth = this.sankey.nodeWidth();
    var links = this.sankey.links();
        
    links.forEach((d, i) => {
      var startX = d.source.x + nodeWidth;
      var endX = d.target.x;
      
      var startY = d.source.y + d.sy + d.dy / 2;
      var endY = d.target.y + d.ty + d.dy / 2;
      
      d3.select(el).select("svg g")
        .append("text")
        .attr("text-anchor", "middle")
        .attr("alignment-baseline", "middle")
        .attr("x", startX + ((endX - startX) / 2))
        .attr("y", startY + ((endY - startY) / 2))
        .text(d.value);
    })
  }
')