networkD3: Как назначить цвет в Sankey Chart для узла без вывода

В моем примере я хотел бы назначить темный цвет (не зеленый) узлу с окончанием сеанса. Но я обнаружил, что цвет для узлов без вывода не назначен должным образом. Почему-то для такой группы используется следующий цвет из таблицы с узлами, группами и цветом.

введите здесь описание изображения

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

person iomedee    schedule 17.03.2020    source источник


Ответы (1)


Узлы в 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

введите здесь описание изображения

person CJ Yetman    schedule 17.03.2020