R networkD3 sankey - 将所有链接的颜色更改为蓝色

R networkD3 sankey - change the colors for all links to blue

这是我使用 networkD3::sankeyNetwork()

制作桑基图的示例代码
library("networkD3")

a <- read.csv(header = TRUE, text = "
date,Data Center,Customer,companyID,source,target,value 
6/1/2021,dcA,customer1,companyID1,open_list_view_1,open_card_2,1 
6/1/2021,dcA,customer1,companyID1,open_card_2,edit_card_3,1
6/1/2021,dcA,customer1,companyID1,edit_card_3,save_card_4,1 
6/1/2021,dcA,customer1,companyID1,save_card_4,back_to_card_list_5,2 
6/1/2021,dcA,customer1,companyID1,back_to_card_list_5,show_more_6,1
6/1/2021,dcA,customer1,companyID1,show_more_6,view_introduction_7,1
6/1/2021,dcA,customer1,companyID1,view_introduction_7,scroll_down_8,2
6/2/2021,dcA,customer2,companyID2,open_list_view_1,open_card_2,3
6/2/2021,dcA,customer2,companyID2,open_card_2,edit_card_3,1
6/2/2021,dcA,customer2,companyID2,edit_card_3,save_card_4,4
6/2/2021,dcA,customer2,companyID2,save_card_4,back_to_card_list_5,2 
6/2/2021,dcA,customer2,companyID2,back_to_card_list_5,show_more_6,1
6/2/2021,dcA,customer2,companyID2,show_more_6,view_introduction_7,1
6/2/2021,dcA,customer2,companyID2,view_introduction_7,scroll_down_8,5
")

node_names <- unique(c(as.character(a$source), as.character(a$target)))
nodes <- data.frame(name = node_names)
links <- data.frame(source = match(a$source, node_names) - 1,
                    target = match(a$target, node_names) - 1,
                    value = a$value)

sankeyNetwork(Links = links, Nodes = nodes, Source = "source",
              Target = "target", Value = "value", NodeID = "name",
              sinksRight = FALSE)

我想将所有链接的颜色更改为蓝色。我认为这个论点 'linkgroups' 可能会有所帮助。

向您的 links data.frame 添加一列,为每个 link 指定组(在您的情况下,它们都在同一组中)。然后添加一个自定义色阶命令,该命令将为该组指定“蓝色”。然后告诉 sankeyNetwork() 函数您的 links data.frame 中指定 link 组的列的名称,并将它传递给您自定义 colorScale.

library("networkD3")

data <- read.csv(header = TRUE, text = "
date,Data Center,Customer,companyID,source,target,value 
6/1/2021,dcA,customer1,companyID1,open_list_view_1,open_card_2,1 
6/1/2021,dcA,customer1,companyID1,open_card_2,edit_card_3,1
6/1/2021,dcA,customer1,companyID1,edit_card_3,save_card_4,1 
6/1/2021,dcA,customer1,companyID1,save_card_4,back_to_card_list_5,2 
6/1/2021,dcA,customer1,companyID1,back_to_card_list_5,show_more_6,1
6/1/2021,dcA,customer1,companyID1,show_more_6,view_introduction_7,1
6/1/2021,dcA,customer1,companyID1,view_introduction_7,scroll_down_8,2
6/2/2021,dcA,customer2,companyID2,open_list_view_1,open_card_2,3
6/2/2021,dcA,customer2,companyID2,open_card_2,edit_card_3,1
6/2/2021,dcA,customer2,companyID2,edit_card_3,save_card_4,4
6/2/2021,dcA,customer2,companyID2,save_card_4,back_to_card_list_5,2 
6/2/2021,dcA,customer2,companyID2,back_to_card_list_5,show_more_6,1
6/2/2021,dcA,customer2,companyID2,show_more_6,view_introduction_7,1
6/2/2021,dcA,customer2,companyID2,view_introduction_7,scroll_down_8,5
")

node_names <- unique(c(as.character(data$source), as.character(data$target)))
nodes <- data.frame(name = node_names)
links <- data.frame(source = match(data$source, node_names) - 1,
                    target = match(data$target, node_names) - 1,
                    value = data$value)

links$linkgroup <- "linkgrp"

colourScale <- 
  'd3.scaleOrdinal()
     .domain(["linkgrp"])
     .range(["blue"].concat(d3.schemeCategory20))'

sankeyNetwork(Links = links, Nodes = nodes, Source = "source",
              Target = "target", Value = "value", NodeID = "name",
              sinksRight = FALSE, 
              LinkGroup = "linkgroup", 
              colourScale = colourScale)