fbreitwieser / sankeyD3

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

Position of nodes on the x axis (missing data) #22

Closed hoelzer closed 4 years ago

hoelzer commented 4 years ago

Firstly, thanks a lot for this great visualization method!

Secondly, I have a problem that is related to missing ranks of a (virus) taxonomy.

TSV Input

10  root    Viruses unclassified
2   root    Viruses Caudovirales    Podoviridae Autographivirinae   T7likevirus
1   root    Viruses Caudovirales    Myoviridae

As you can see, I dont have all ranks available for my taxonomy. Now, I translate this table into JSON.

JSON input

{"nodes":[
    {"name":"Viruses","id":0},
    {"name":"unclassified","id":1},
    {"name":"Caudovirales","id":2},
    {"name":"Podoviridae","id":3},
    {"name":"Autographivirinae","id":4},
    {"name":"T7likevirus","id":5},
    {"name":"Myoviridae","id":6}
    ],"links":[
    {"source":0,"target":1,"value":10},
    {"source":0,"target":2,"value":3},
    {"source":2,"target":3,"value":2},
    {"source":3,"target":4,"value":2},
    {"source":4,"target":5,"value":2},
    {"source":2,"target":6,"value":1}
    ]}

and plot this using

sankeyNetwork(Links = Taxonomy$links, Nodes = Taxonomy$nodes, Source = "source", Target = "target", Value = "value", NodeID = "name", units = "count", fontSize = 22, nodeWidth = 30, nodeShadow = TRUE, nodePadding = 30, nodeStrokeWidth = 1, nodeCornerRadius = 10, dragY = TRUE, dragX = TRUE, numberFormat = ",.3g") %>% saveNetwork(file = 'sankey.html')

That results in this figure: Screenshot from 2020-03-24 18-23-52

That I can manually adjust to look like this: Screenshot from 2020-03-24 18-24-46

Because actually, Podoviridae and Myoviridae belong to the same column in the plot (family).

My question: Is there a way to automatically assign nodes to the correct column like they are written in the TSV?

hoelzer commented 4 years ago

I also tried adding a pos column to my nodes like this:

> Taxonomy$nodes
               name id pos
1           Viruses  0   0
2      unclassified  1   1
3      Caudovirales  2   1
4       Podoviridae  3   2
5 Autographivirinae  4   3
6       T7likevirus  5   4
7        Myoviridae  6   1

and adding to my command

NodePosX = "pos"

but this did not help (no change)

hoelzer commented 4 years ago

I found a solution by simply adding

align = "left"

to the sankey code.