How to color groups in networkD3's sankeyNetwork?

后端 未结 2 926
既然无缘
既然无缘 2021-01-03 05:33

My nodes consists of names and groups yet I can\'t seem to implement distinct colors for groups in my sankey diagram. The colors are either all blue with defaults or all bla

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-03 05:44

    library(networkD3)
    source <- c(0,1,2,3,4,5)
    target <- c(2,2,2,3,1,0)
    value <- c(33,44,55,66,77,88)
    
    sankeydata <- data.frame(source,target, value)
    
    names <- c('a', 'b', 'c', 'd', 'e', 'f')
    id <- c(0,1,2,3,4,5)
    group <- c(1,1,1,2,2,2)
    
    sankeyNodes <- data.frame(names,id, group)
    
    
    sankeyNetwork(Links = sankeydata, Nodes = sankeyNodes, Source = "source",
             Target = "target", Value = "value", NodeID = "names", NodeGroup = "group", fontSize = 12, nodeWidth = 30)
    

    I would expect two colors ( since there are two groups), but no colors return. I have the same issue as OP.

    The help text suggests NodeGroup is responsible for the color.

    If you run a similar code for another graph in library(networkD3):

    #same data
    forceNetwork(Links = sankeydata, Nodes = sankeyNodes , Source = "source",
             Target = "target", Value = "value", NodeID = "names",
             Group = "group", opacity = 0.8, zoom = TRUE)
    

    Plots two different colors for in the network graph.

提交回复
热议问题