Fixing the order of a Sankey flow graph in R / networkD3 package

后端 未结 1 1495
借酒劲吻你
借酒劲吻你 2021-01-12 21:11

I would like to visualize a rank change (i.e. change of the relative order) of US states using a Sankey flow graph. I\'m using the networkd3 package and came up with the fol

相关标签:
1条回答
  • 2021-01-12 21:14

    If you set iterations = 0 in sankeyNetwork(), you will effectively disable the algorithm which automatically determines the node placement (which is the primary purpose of the sankeyNetwork() function), and the nodes will be placed in the order that they appear in the Nodes dataframe.

    library(networkD3)
    
    states <- state.name[1:10]
    ranks <- sample.int(10)
    
    nodes <- data.frame(name = c(states, states[ranks]))
    links <- data.frame(source = 1:10 - 1, target = order(ranks) + 10 - 1, value = 1)
    
    sankeyNetwork(Links = links, Nodes = nodes, Source = 'source', 
                  Target = 'target', Value = 'value', NodeID = 'name',
                  iterations = 0)
    

    0 讨论(0)
提交回复
热议问题