I am currently creating a Sankey-diagram using the networkD3
package in R.
There is one initial node, let's call it node 0, that is connected to all the other nodes. Furthermore, I have a grouping variable, let's say gender, that needs to be visually distinguishable. Ideally, I would like to remove node 0 and all its edges from the plot without affecting the rest of the graph (i.e. node 0 and its connections should simply be invisible). Alternatively, it would be helpful if I could define a special color and opacity for the connections with node 0. While this was possible by defining a group for all rows where the source column shows 0, this would then replace the colour scheme for the other grouping variable in my data.
Here is my code using some fictional data:
library(dplyr)
library(networkD3)
links <- structure(list(source = c(0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 0, 0,
2, 2, 0, 0, 3, 3, 2, 0, 0, 3, 3, 4, 4, 0, 0, 3, 5, 5),
target = c(1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4,
5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6),
gender = c("Female", "Male", "Female", "Male", "Female",
"Male", "Female", "Male", "Female", "Male", "Female",
"Male", "Female", "Male", "Female", "Male", "Female",
"Male", "Male", "Female", "Male", "Female", "Male",
"Female", "Male", "Female", "Male", "Female", "Female", "Male"),
value = c(83, 64, 33, 6, 41, 59, 60, 6, 19, 70, 49, 86, 31, 97,
83, 96, 53, 69, 57, 64, 16, 47, 51, 86, 39, 30, 13, 27, 84, 71),
group = c("A","A", "B", "B", "A", "A", "B", "B", "B", "B", "A", "A", "B", "B",
"A", "A", "B", "B", "B", "A", "A", "B", "B", "B", "B", "A", "A",
"B", "B", "B"),
IDsource = c(0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 0,
0, 2, 2, 0, 0, 3, 3, 2, 0, 0, 3,
3, 4, 4, 0, 0, 3, 5, 5),
IDtarget = c(1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4,
4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6)),
class = "data.frame", row.names = c(NA, -30L))
nodes <- data.frame(
name = c(as.character(links$source),
as.character(links$target)) %>% unique())
nodes$group <- as.factor(c("node_col"))
sankey_cols <- 'd3.scaleOrdinal() .domain(["Female", "Male", "node_col"]) .range([d3.rgb(100,150,150, 0.5),
d3.rgb(150,150,100, 0.5),
d3.rgb(200,100,150, 0.5)])'
p <- sankeyNetwork(Links = links, Nodes = nodes,
Source = "IDsource", Target = "IDtarget",
colourScale = sankey_cols,iterations = 4,fontSize = 15,
LinkGroup = "gender",NodeGroup = "group",
Value = "value", NodeID = "name", sinksRight = FALSE)
p
Furthermore, if this is still in the scope of this question, is it possible to add a colour-legend to the graph?