fbreitwieser / sankeyD3

D3 Sankey Network Graphs from R
67 stars 27 forks source link

Edge data #7

Open psychemedia opened 7 years ago

psychemedia commented 7 years ago

Does the edge data require that the nodes are identified as consecutive integers, starting from 0?

eg this works:

cc=data.frame(source=c(0,1,2),target=c(3,4,5),value=c(10,15,20))
cn=data.frame(name=c('a','b','c','d','e','f'))
sankeyNetwork(Links = cc, Nodes = cn, Source = "source", Target = "target", Value = "value", NodeID = "name",  fontSize = 12, nodeWidth = 30)

but if I try:

 cc=data.frame(source=c(6,1,2),target=c(3,4,5),value=c(10,15,20))

or

cc=data.frame(source=c(0,1,2),target=c(4,5,6),value=c(10,15,20))

or

cc=data.frame(source=c(0,1,2),target=c('x','y','z'),value=c(10,15,20))

the chart doesn't render?

UPDATE: I think @timelyportfolio's Sankey plot uses the following to get round the index issue:

//FROM: http://bl.ocks.org/timelyportfolio/79350d7475a5709b051e
//it appears d3 with force layout wants a numeric source and target
//so loop through each link replacing the text with its index from node
links.forEach(function (d, i) {
    links[i].source = nodes.indexOf(links[i].source);
    links[i].target = nodes.indexOf(links[i].target);
});
fbreitwieser commented 6 years ago

Indeed currently it does require that. I'll think about the work-around from @timelyportfolio .