iomedee
iomedee

Reputation: 393

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

In my example I would like to assign dark color (not green) to the node with the session end. But I find out that the color for nodes without any output is not properly assigned. For some reason for such group is used next color from table with nodes, groups and color.

enter image description here

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

Upvotes: 0

Views: 750

Answers (1)

CJ Yetman
CJ Yetman

Reputation: 8848

Nodes in sankeyNetwork are coloured per group, not per node, so you need to set colorJS with a set of colours for each group, for instance...

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

which gives you...

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

enter image description here

Upvotes: 1

Related Questions