0

I have been using the sankeyD3 package to create SankeyNetworks and the 'NodePosX' feature isn't working for me yet. The 'NodePosX' feature is not in the 'networkD3' package but it is in the 'sankeyD3' package.

To help illustrate the 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.

We're aiming for something like this, but without the small line from 'Opted-Out' to 'Activated':enter image description here

library(devtools)
devtools::install_github("fbreitwieser/sankeyD3")
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)
CJ Yetman
  • 8,373
  • 2
  • 24
  • 56
Nevil
  • 161
  • 1
  • 11

1 Answers1

2

Assuming the last row in your links data frame is only there to force the plot to look the way you want and not part of the actual data you want to plot, you can achieve this with using the sinksRight = FALSE parameter.

library(networkD3)

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

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

sankeyNetwork(Links = links, Nodes = nodes, Source = "source",
              Target = "target", Value = "value", NodeID = "name",
              units = "TWh", fontSize = 12, nodeWidth = 30, sinksRight = FALSE)

enter image description here

CJ Yetman
  • 8,373
  • 2
  • 24
  • 56
  • Hello CJ. Thanks for the swift suggestion. The following adapted code does indeed deliver what I wanted. – Nevil Jul 15 '18 at 07:17
  • I've just discovered that the sinksRight = FALSE option only stops nodes drifting right. I've discovered that it means that all nodes must start on the left. I have a set of data for which this isn't ideal. Therefore, I'm still wanting to know why the NodePosX option doesn't work as advertised from the sankeyD3 package. – Nevil Jul 16 '18 at 18:46