How to control node labels in Sankey diagram

How to control node labels in Sankey diagram



I have a Sankey diagram I'm creating in R. It produces the diagram but the node names are placed on the left of the nodes for the right side nodes. I would like all names to be on the right so the real names (which are longer) don't overlap with each other.



Below is a complete set of working code that will generate the diagram I am currently working with.


library(networkD3)

nodes <- data.frame('name' =
c('Node0','Node1','Node2','Node3','Node4','Node5','Node6',
'Node7','Node8','Node9','Node10','Node11','Node12','Node13',
'Node14','Node15','Node16','Node17','Node18','Node19',
'Node20','Node21','Node22','Node23','Node24','Node25',
'Node26','Node27','Node28','Node29','Node30','Node31',
'Node32','Node33'))

links = as.data.frame(matrix(c(
0, 3,140,
0, 4,140,
0, 5,140,
0, 6,140,
1, 3,140,
1, 4,140,
1, 5,140,
1, 6,140,
2, 3,140,
2, 4,140,
2, 5,140,
2, 6,140,
3, 7,130,
3, 8,130,
3, 9,50,
3,10,50,
3,11,50,
4,12,140,
4,13,100,
4,14,100,
4,15,80,
5,16,150,
5,17,150,
5,18,60,
5,19,60,
6,20,180,
6,21,80,
6,22,80,
6,23,80,
7,24,13,
7,33,13,
7,31,104,
8,24,13,
8,33,13,
8,26,52,
8,27,52,
9,24,10,
9,33,10,
9,29,30,
9,30,30,
10,24,10,
10,33,10,
10,29,30,
10,30,30,
11,24,10,
11,33,10,
11,29,30,
11,30,30,
12,24,16,
12,33,16,
12,26,36,
12,27,36,
12,28,36,
13,24,10,
13,33,10,
13,26,30,
13,27,30,
13,28,30,
14,24,10,
14,33,10,
14,26,30,
14,27,30,
14,28,30,
15,24,10,
15,33,10,
15,31,60,
16,24,30,
16,33,30,
16,32,90,
17,24,30,
17,33,30,
17,32,90,
18,24,10,
18,33,10,
18,25,40,
19,24,30,
19,33,30,
20,24,90,
20,33,90,
21,33,80,
22,24,10,
22,33,10,
22,29,30,
22,30,30,
23,24,40,
23,33,40),
byrow = TRUE, ncol = 3))

names(links) = c("source", "target", "value")
sankeyNetwork(Links = links, Nodes = nodes,
Source = "source", Target = "target",
Value = "value", NodeID = "name",
fontSize= 15, nodeWidth = 20,
colourScale = JS("d3.scaleOrdinal(d3.schemeCategory20b);"))




1 Answer
1



You can set the text-anchor after creating the sankey network plot by adding some JavaScript using the htmlwidgets package. You may also want to adjust the left margin if you want the text to be on right side of the right side nodes.


library(htmlwidgets)

sn <- sankeyNetwork(Links = links, Nodes = nodes,
Source = "source", Target = "target",
Value = "value", NodeID = "name",
fontSize= 15, nodeWidth = 20, margin = list(left = 100),
colourScale = JS("d3.scaleOrdinal(d3.schemeCategory20b);"))

onRender(
sn,
'
function(el, x)
d3.selectAll(".node text").attr("text-anchor", "begin").attr("x", 20);

'
)






Thank you so much. I tried upvoting your answer but I don't have enough reputation yet. This did exactly what I was looking for.

– VPMACH
Aug 29 '17 at 17:20






When a sankey plot is in a tab (other than the first), (using rmarkdown.rstudio.com/html_document_format.html#tabbed_sections), the above method does not work. Do you happen to know a way to overcome that problem?

– George Dontas
Mar 26 '18 at 10:07






create a new question with a minimal reproducible example and I'll take a look at

– CJ Yetman
Mar 26 '18 at 14:35






Here is the link to the new question: stackoverflow.com/questions/49509228/…

– George Dontas
Mar 27 '18 at 9:32



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)