SchmidtPaul / sankeyD3plus

D3 JavaScript Sankey Diagrams in R
https://schmidtpaul.github.io/sankeyD3plus/
GNU General Public License v3.0
2 stars 0 forks source link

Add examples #1

Open SchmidtPaul opened 1 year ago

SchmidtPaul commented 1 year ago

The Examples page should have examples for

SchmidtPaul commented 1 year ago

Hey @kenneditodd, could I ask you for your input on this? Can you install the package and recreate the examples? What else are you interested in?

kenneditodd commented 1 year ago

@SchmidtPaul Your examples page says 404 - page not when you click on the link from the main README. However, the link from this page works and I was able to replicate all the examples. Things off the top of my head is i need the nodes to almost repel eachother for a cleaner display. Also, when zooming and dragging nodes a reset button would be nice. Kinda like what you see with plotly graphs.

kenneditodd commented 1 year ago

So, using the following code i generated this graph.

# Links table
links <- tribble(
  ~source, ~target, ~value, ~linkcolor,
  0,       1,       3510683,    "gray", # ccs to extract hifi
  1,       2,       3089255,    "gray", # extract hifi to lima passed
  1,       3,       421428,     "gray", # extract hifi to lima failed
  2,       4,       3036889,    "gray", # lima passed to refine passed
  2,       5,         52366,    "gray", # lima passed to refine fail
  4,       6,       157826,     "gray", # refine passed to cluster HQ
  4,       7,           44,     "gray", # refine passed to cluster LQ
  6,       8,        57528,     "gray", # cluster HQ to collapse
  8,       9,        57528,     "gray", # collapse to classify       
  9,      10,        40201,     "gray", # classify to filter   
  6,      11,       157761,     "gray", # cluster HQ to mapped          
  6,      12,           65,     "gray", # cluster HQ to did not map
 11,      13,       157761,     "gray"  # mapped to bambu
)

# Nodes table
nodes <- tribble(
  ~id, ~label,                  ~nodecolor,    ~yorder,
  0,   "CCS",                   "skyblue",           1,
  1,   "Extract HiFi",          "skyblue",           1,
  2,   "Lima Passed",           "skyblue",           1,
  3,   "Lima Failed",           "black",             2,
  4,   "Refine Passed",         "skyblue",           1,
  5,   "Refine Failed",         "black",             2,
  6,   "Cluster HQ",            "skyblue",           2,
  7,   "Cluster LQ",            "black",             1,
  8,   "Collapse",              "red",               1,
  9,   "Classify",              "red",               1,
  10,  "Filter",                "red",               1,
  11,  "Mapped",                "gold",              2,
  12,  "Did not map",           "black",             3,
  13,  "Bambu",                 "gold",              2
)

# Plot
sankeyNetwork(
  Links = links,
  Source = "source",
  Target = "target",
  Value = "value",
  linkColor = "linkcolor",
  Nodes = nodes,
  NodeID = "label",
  NodeColor = "nodecolor",
  NodePosY = "yorder",
  numberFormat = ",.0f"
)
image

But I want it to be more like this without manually adjusting (it will be in shiny app). The width of the link is equal to the size of the node. I was wondering if the width of the link could be adjusted. It would be nice to pass a value, maybe a percentage of the node size, to the links table. Also I have crowding that I can manually adjust but even after I play around with y-axis ordering, it still doesn't look as good as when I manually move them.

image
SchmidtPaul commented 1 year ago

Thanks for your input @kenneditodd !

  1. I fixed the 404.
  2. I think repelling nodes should work via increasing the nodePadding. I have a reprex below where I do that and also change a few yorder in your example to obtain an improved plot without needing to manually drag around stuff.
  3. I'm afraid I actually don't have a big focus on using these in a shiny app but I think you could just set up a reset button so that the sankeyNetwork() function itself runs again.
library(sankeyD3plus)
library(tibble)

# Links table
links <- tribble(
  ~source, ~target, ~value, ~linkcolor,
  0,       1,       3510683,    "gray", # ccs to extract hifi
  1,       2,       3089255,    "gray", # extract hifi to lima passed
  1,       3,       421428,     "gray", # extract hifi to lima failed
  2,       4,       3036889,    "gray", # lima passed to refine passed
  2,       5,         52366,    "gray", # lima passed to refine fail
  4,       6,       157826,     "gray", # refine passed to cluster HQ
  4,       7,           44,     "gray", # refine passed to cluster LQ
  6,       8,        57528,     "gray", # cluster HQ to collapse
  8,       9,        57528,     "gray", # collapse to classify       
  9,      10,        40201,     "gray", # classify to filter   
  6,      11,       157761,     "gray", # cluster HQ to mapped          
  6,      12,           65,     "gray", # cluster HQ to did not map
  11,      13,       157761,    "gray"  # mapped to bambu
)

# Nodes table
nodes <- tribble(
  ~id, ~label,                  ~nodecolor,    ~yorder,
  0,   "CCS",                   "skyblue",           1,
  1,   "Extract HiFi",          "skyblue",           1,
  2,   "Lima Passed",           "skyblue",           2, # changed
  3,   "Lima Failed",           "black",             1, # changed
  4,   "Refine Passed",         "skyblue",           2, # changed
  5,   "Refine Failed",         "black",             1, # changed
  6,   "Cluster HQ",            "skyblue",           2,
  7,   "Cluster LQ",            "black",             1,
  8,   "Collapse",              "red",               1,
  9,   "Classify",              "red",               1,
  10,  "Filter",                "red",               1,
  11,  "Mapped",                "gold",              2,
  12,  "Did not map",           "black",             3,
  13,  "Bambu",                 "gold",              2
)

# Plot
sankeyNetwork(
  Links = links,
  Source = "source",
  Target = "target",
  Value = "value",
  linkColor = "linkcolor",
  Nodes = nodes,
  NodeID = "label",
  NodeColor = "nodecolor",
  NodePosY = "yorder",
  numberFormat = ",.0f",
  nodePadding = 50
)

Created on 2023-08-04 with reprex v2.0.2