networkD3:如何将桑基图中的颜色分配给没有输出的节点

networkD3: How to assign color in sankey chart to the node without output

在我的示例中,我想为会话结束的节点分配深色(不是绿色)。但是我发现没有正确分配没有任何输出的节点的颜色。出于某种原因,此类组使用 table 中的下一种颜色以及节点、组和颜色。

library(networkD3)
library(data.table)
library(stringi)

nodes <- data.table(name = c("Installed(1)", "Contact Info Page(2)", "Trial Period Started(2)", 
                             "Uninstalled before trial(2)", "Subscription Page(3)", "Trial Period Started(3)",    
                             "Uninstalled before trial(3)", "Trial Period Started(4)", "Uninstalled before trial(4)",
                             "Installed(5)", "Session end(2)", "Session end(3)",             
                             "Session end(4)", "Session end(5)", "Trial Period Started(6)" ))
nodes[stri_detect_regex(name, 'Session end'), `:=`(group = 'Session end', color = '#212121') ]
nodes[stri_detect_regex(name, 'Uninstalled before trial'), `:=`(group = 'Uninstalled', color = '#36474f') ]
nodes[stri_detect_regex(name, 'Installed'), `:=`(group = 'Installed', color = '#3949ab') ]
nodes[stri_detect_regex(name, 'Contact Info Page'), `:=`(group = 'Contact Info Page', color = '#fe5720') ]
nodes[stri_detect_regex(name, 'Subscription Page'), `:=`(group = 'Subscription Page', color = '#f44135') ]
nodes[stri_detect_regex(name, 'Trial Period Started'), `:=`(group = 'Trial Period Started', color = '#4caf50') ]

links <- data.table(from = c(0, 0, 0, 0, 1, 1, 1, 1, 2, 3, 4, 4, 4, 5, 6, 7, 8, 8, 9), 
                    to = c(1,10,2,3,11,4,5,6,11,11,12,7,8,12,12,13,9,13,14),   
                    n = c(29,5,1,2,1,18,1,9,1,2,1,13,4,1,9,13,1,3,1))

colors <- paste(nodes$color, collapse = '", "')
colorJS <- paste('d3.scaleOrdinal(["', colors, '"])')

sn <- sankeyNetwork(Links = links, Nodes = nodes[, .(name, group)], Source = "from",
                    Target =  "to", Value = "n", NodeID = "name", sinksRight = FALSE,
                    NodeGroup = "group", colourScale = colorJS, fontSize = 13)

sn

sankeyNetwork 中的节点按组着色,而不是按节点着色,因此您需要为每个组设置 colorJS 一组颜色,例如...

colors <- paste(unique(nodes[, .(group, color)])$color, collapse = '", "')
colorJS <- paste('d3.scaleOrdinal(["', colors, '"])')

这给了你...

library(networkD3)
library(data.table)
library(stringi)

nodes <- data.table(name = c("Installed(1)", "Contact Info Page(2)", "Trial Period Started(2)", 
                             "Uninstalled before trial(2)", "Subscription Page(3)", "Trial Period Started(3)",    
                             "Uninstalled before trial(3)", "Trial Period Started(4)", "Uninstalled before trial(4)",
                             "Installed(5)", "Session end(2)", "Session end(3)",             
                             "Session end(4)", "Session end(5)", "Trial Period Started(6)" ))
nodes[stri_detect_regex(name, 'Session end'), `:=`(group = 'Session end', color = '#212121') ]
nodes[stri_detect_regex(name, 'Uninstalled before trial'), `:=`(group = 'Uninstalled', color = '#36474f') ]
nodes[stri_detect_regex(name, 'Installed'), `:=`(group = 'Installed', color = '#3949ab') ]
nodes[stri_detect_regex(name, 'Contact Info Page'), `:=`(group = 'Contact Info Page', color = '#fe5720') ]
nodes[stri_detect_regex(name, 'Subscription Page'), `:=`(group = 'Subscription Page', color = '#f44135') ]
nodes[stri_detect_regex(name, 'Trial Period Started'), `:=`(group = 'Trial Period Started', color = '#4caf50') ]

links <- data.table(from = c(0, 0, 0, 0, 1, 1, 1, 1, 2, 3, 4, 4, 4, 5, 6, 7, 8, 8, 9), 
                    to = c(1,10,2,3,11,4,5,6,11,11,12,7,8,12,12,13,9,13,14),   
                    n = c(29,5,1,2,1,18,1,9,1,2,1,13,4,1,9,13,1,3,1))

colors <- paste(unique(nodes[, .(group, color)])$color, collapse = '", "')
colorJS <- paste('d3.scaleOrdinal(["', colors, '"])')

sn <- sankeyNetwork(Links = links, Nodes = nodes[, .(name, group)], Source = "from",
                    Target =  "to", Value = "n", NodeID = "name", sinksRight = FALSE,
                    NodeGroup = "group", colourScale = colorJS, fontSize = 13)

sn