I generating a Sankey diagram using the R code below. The problem is that the nodes don't line up as one would expect. For example, the two of the M3 nodes line up with the M24 nodes groupinstead of grouping together with the other M3 nodes. I would appreciate any suggestion on how to control the horizontal positions of the nodes in R. Thanks.
library(networkD3)
# Define nodes and links data frames
nodes <- data.frame(
'name' = c("BL", "M3", "M3", "M3", "M3", "M12", "M12", "M12", "M12", "M18", "M18", "M18", "M18", "M24", "M24", "M24", "M24"),
'group' = as.factor(c("g","dgr","lgr","g","r","dgr","lgr","g","r","dgr","lgr","g","r","dgr","lgr","g","r"))
)
links <- data.frame(
'source' = c(0, 0, 0, 0, 3, 3, 3, 3, 4, 4, 4, 4, 7, 7, 7, 7, 8, 8, 8, 8, 11, 11, 11, 12, 12, 12, 12),
'target' = c(1, 2, 3, 4, 5, 6, 7, 8, 5, 6, 7, 8, 9, 10, 11, 12, 9, 10, 11, 12, 13, 15, 16, 13, 14, 15, 16),
'value' = c(70, 80, 1504, 843, 14, 13, 695, 317, 12, 44, 87, 271, 5, 10, 360, 183, 6, 12, 69, 219, 7, 186, 108, 2, 5, 46, 138),
'group' = as.factor(c("dgr_","lgr_","g_","r_","dgr_","lgr_","g_","r_","dgr_","lgr_","g_","r_","dgr_","lgr_","g_","r_","dgr_","lgr_","g_","r_","dgr_","g_","r_","dgr_","lgr_","g_","r_"))
)
# Define color scale
my_color <- 'd3.scaleOrdinal().domain(["g","dgr","lgr","r","g_","dgr_","lgr_","r_"]) .range(["green", "darkgrey", "lightgrey","red", "green", "darkgrey", "lightgrey","red"])'
# Plot Sankey diagram
sankey_R = sankeyNetwork(
Links = links, Nodes = nodes,
Source = "source", Target = "target",
Value = "value", NodeID = "name",
fontSize = 12, nodeWidth = 20,
colourScale = my_color,
NodeGroup = "group", LinkGroup = "group"
)
sankey_R
I tried plot_ly() insted of sankeyNetwork(), but the results was identical
I found a solution. If I generate the Sankey with plot_ly, and output as .html then I can drag the nodes around and rearrange them as I wish.