R Sankey Network如何禁用降序排序(重叠)

时间:2019-03-16 01:55:38

标签: r sankey-diagram networkd3

enter image description here

看看上面ConfirmedUnconfirmed的输出如何重叠?我不要看来最终值VendorTBDInternalNo Problem Found是以降序排列的,从而导致了此问题。

理想情况下,Unconfirmed -> TBD的流应为图像底部的直线。奇怪的是,最初几次运行脚本时,它是这样显示的!但这不再出现了吗?

有什么办法可以做我想要的吗?这是我的代码:

library(tidyverse)
library(networkD3)

# Generate Sankey Data ---------------------------------------------------------
source <- c("Barcode Scanner", "Cell Enrichment", "Consumables", "Contamination", "Cosmetic", "Dimensional", "Display", "Heaters", "Labels", "Missing Component", "Pumps", "Vials", "Barcode Scanner", "Cell Enrichment", "Consumables", "Contamination", "Cosmetic", "Dimensional", "Display", "Heaters", "Labels", "Missing Component", "Pumps", "Vials", "Confirmed", "Confirmed", "Confirmed", "Unconfirmed")
target <- c("Confirmed", "Confirmed", "Confirmed", "Confirmed", "Confirmed", "Confirmed", "Confirmed", "Confirmed", "Confirmed", "Confirmed", "Confirmed", "Confirmed", "Unconfirmed", "Unconfirmed", "Unconfirmed", "Unconfirmed", "Unconfirmed", "Unconfirmed", "Unconfirmed", "Unconfirmed", "Unconfirmed", "Unconfirmed", "Unconfirmed", "Unconfirmed", "Vendor", "Internal", "No Problem Found", "TBD")
values <- c("18", "8", "20", "15", "7", "17", "20", "14", "9", "16", "11", "20", "8", "7", "8", "8", "8", "6", "2", "5", "4", "8", "6", "6", "98", "53", "24", "76")

# Setup Sankey Network ----------------------------------------------------------
nodes <- data.frame(name = unique(c(source, target)))
links <- data.frame(source = match(source, nodes$name) - 1,
                    target = match(target, nodes$name) - 1,
                    value = values)
sn <- sankeyNetwork(Links = links, Nodes = nodes, Source = "source", 
              Target = "target", Value = "value", NodeID = "name", 
              units = "Complaints", fontSize = 12, nodeWidth = 20)

# Fix issues with Firefox rendering --------------------------------------------
htmlwidgets::onRender(sn, 'document.getElementsByTagName("svg")[0].setAttribute("viewBox", "")')

1 个答案:

答案 0 :(得分:1)

As explained in a similar question这提供了解决方案:iterations = 0

library(tidyverse)
library(networkD3)

# Generate Sankey Data ---------------------------------------------------------
source <- c("Barcode Scanner", "Cell Enrichment", "Consumables", "Contamination", "Cosmetic", "Dimensional", "Display", "Heaters", "Labels", "Missing Component", "Pumps", "Vials", "Barcode Scanner", "Cell Enrichment", "Consumables", "Contamination", "Cosmetic", "Dimensional", "Display", "Heaters", "Labels", "Missing Component", "Pumps", "Vials", "Confirmed", "Confirmed", "Confirmed", "Unconfirmed")
target <- c("Confirmed", "Confirmed", "Confirmed", "Confirmed", "Confirmed", "Confirmed", "Confirmed", "Confirmed", "Confirmed", "Confirmed", "Confirmed", "Confirmed", "Unconfirmed", "Unconfirmed", "Unconfirmed", "Unconfirmed", "Unconfirmed", "Unconfirmed", "Unconfirmed", "Unconfirmed", "Unconfirmed", "Unconfirmed", "Unconfirmed", "Unconfirmed", "Vendor", "Internal", "No Problem Found", "TBD")
values <- c("18", "8", "20", "15", "7", "17", "20", "14", "9", "16", "11", "20", "8", "7", "8", "8", "8", "6", "2", "5", "4", "8", "6", "6", "98", "53", "24", "76")

# Setup Sankey Network ----------------------------------------------------------
nodes <- data.frame(name = unique(c(source, target)))
links <- data.frame(source = match(source, nodes$name) - 1,
                    target = match(target, nodes$name) - 1,
                    value = values)
sn <- sankeyNetwork(Links = links, Nodes = nodes, Source = "source", 
                    Target = "target", Value = "value", NodeID = "name", 
                    units = "Complaints", fontSize = 12, nodeWidth = 20,
                    iterations = 0)

# Fix issues with Firefox rendering --------------------------------------------
htmlwidgets::onRender(sn, 'document.getElementsByTagName("svg")[0].setAttribute("viewBox", "")')