如何自定义节点和使用R在sankeyNetwork中的边缘位置

时间:2017-09-22 04:52:11

标签: r sankey-diagram networkd3

来自networkD3软件包的sankeyNetwork在定位节点时大多数时候非常聪明,但有时我想将节点水平放置到另一个位置。 还有其他场合我想垂直移动边缘的起点。以下面的屏幕截图为例:

enter image description here

对于节点,我想将左边第二列的B3从右边移动到第四列。同样,我想将B4从第二列从右侧移动到第五列,从左侧开始。

对于边缘,我想将第一个边缘(B1-> B11)的起点移动到B1的低端。

我的猜测是我需要对源代码进行一些更改并手动输入特定的位置。我在js How to tune horizontal node position in d3 sankeyjs看过这篇文章,但我不知道在R中做什么,旁边我没有找到任何关于改变边缘位置的帖子。

这是可复制的数据和代码:

   load(url("https://github.com/bossaround/question/raw/master/sankeyexample.RData"))
      # nn is the node, ee is the edge, now create the link (ll)      
      ll <- inner_join(ee, nn, by = c("N1"="name")) %>%
        rename(source_ID = ID) %>%
        inner_join(nn, by = c("N2"="name")) %>%
        rename(target_ID = ID) 

      # Create Sankey Plot
       sankeyNetwork(
        Links = ll,
        Nodes = nn,
        Source = "source_ID",
        Target = "target_ID",
        Value = "Value",
        NodeID = "newname",
        fontSize = 12,
        nodeWidth = 40,
        nodePadding = 20,
        NodeGroup = "newname"

      )

提前谢谢!

1 个答案:

答案 0 :(得分:1)

  1. 您可以手动调整节点的垂直位置,因此如果将右上角的B11节点拖到底部,左上方B1节点的链接线将自动调整到该B1节点的底部

  2. 要实现the answer to How to tune horizontal node position in d3 sankey.js?中描述的内容,您只需将sinksRight = FALSE参数添加到sankeyNetwork()来电,但我认为这不会达到您的目的实际上想要。

  3. 目前无法手动调整节点的水平位置。在代码中单独调整它们将非常繁琐,但您可以添加额外的JS以在加载后运行,以使用一些复杂的JS代码(如d3.selectAll(".node").filter(function(d) { return d.name == "B3" }).attr("transform", function(d) { return "translate(" + (d.x - 300) + "," + d.y + ")"; })

  4. 单独调整特定节点的位置