fbreitwieser / sankeyD3

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

Helping make NodePosX work #18

Open NevilHopley opened 6 years ago

NevilHopley commented 6 years ago

I have been using the sankeyD3 package to create SankeyNetworks and the 'NodePosX' feature isn't working for me yet.

To help illustrate the similar problem that I am having, I have edited the example from akraemer007 that was posted here to include the X positions of the nodes (see below) but it's still not working in the way that he had originally wanted, with manual control over the x-position of the 'Opted-Out' node.

Can someone please help identify the issue that I'm missing? Thank you Nevil

library(sankeyD3)
name <- c('Enrolled', 'Opted-Out', 'Invited', 'Activated')
xpos <- c(0, 1, 1, 2)
nodes <- data.frame(name, xpos)

source <- c(0, 0, 2, 1) 
target <- c(1, 2, 3, 3) 
value <- c(20, 80, 60, 0) 
links <- data.frame(source, target, value)
sankeyNetwork(Links = links, Nodes = nodes, Source = "source",
              Target = "target", Value = "value", NodeID = "name",NodePosX = "xpos",
              units = "TWh", fontSize = 12, nodeWidth = 30)
mp8 commented 4 years ago

One thing I have learned while using sankeyNetwork from netorkD3 is node orders (Y axis) is preserved when number of iterations =0. I tried your code with iterations = 0 and seems it preserve the specified NodePosX argument as well.

SchmidtPaul commented 2 years ago

I know I'm three years late but maybe this helps:

nodes <- data.frame(
  name = c("Enrolled", "Opted-Out", "Invited", "Activated"),
  xpos = c(0, 1, 2, 5)
)

links <- data.frame(
  source = c(0, 0, 2, 1),
  target = c(1, 2, 3, 3),
  value = c(20, 80, 80, 20)
)

sankeyD3::sankeyNetwork(
  Links = links,
  Nodes = nodes,
  Source = "source",
  Target = "target",
  Value = "value",
  NodeID = "name",
  fontSize = 12,
  NodePosX = "xpos",
  align = "none"
)

Created on 2021-12-22 by the reprex package (v2.0.1)