1
votes

I am trying to build sankey plot in R using networkD3 library.

I have following data

print(nodes1)

  name
1   as
2  sdf
3  wer
4   xc
5   sd
6   er

print(links)

   source target value
1       0      5   112
2       0      4   848
3       0      4   661
4       0      4    33
5       0      3   291
6       1      5    23
7       1      4   983
8       1      3   859
9       1      3    35
10      2      4   537

So when I try to create sankeyNetwork using following code

sankeyNetwork(Links = links1, Nodes = nodes1,
 Source = "source", Target = "target",
 Value = "value", NodeID = "name",
 fontSize= 12, nodeWidth = 30)

I get following result.

SankeyPlot

However, it is not printing nodes in order.

In my node dataframe, i have "as" is first node, and "sdf" is second node. However, in image, I am getting "as" as first node, and "wer" as second node.

How can I make sure, graph follows order in nodes?

1
sankeyNetwork does not allow nodes to be ordered manaullly/progamatically. I can try to dig out the link later. If you want to set the node ordering you could try riverplot but it is not interactive. - emilliman5
I need interactive graph - Kush Patel

1 Answers

7
votes

If you set iterations = 0, the algorithm that does the auto-positioning of the nodes will essentially not run and leave the nodes in their initial positions...

nodes <- read.table(header = T, text = "
name
as
sdf
wer
xc
sd
er
")

links <- read.table(header = T, text = "
source target value
0      5   112
0      4   848
0      4   661
0      4    33
0      3   291
1      5    23
1      4   983
1      3   859
1      3    35
2      4   537
")

library(networkD3)
sankeyNetwork(Links = links, Nodes = nodes,
              Source = "source", Target = "target",
              Value = "value", NodeID = "name",
              fontSize= 12, nodeWidth = 30, iterations = 0)