【发布时间】:2020-03-17 10:27:52
【问题描述】:
在我的示例中,我想将深色(不是绿色)分配给会话结束的节点。但是我发现没有正确分配没有任何输出的节点的颜色。出于某种原因,此类组使用带有节点、组和颜色的表中的下一个颜色。
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
【问题讨论】:
标签: r sankey-diagram htmlwidgets networkd3