Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing "unused" nodes in sankey network

I am trying to build a sankey network. This is my data and code:

library(networkD3)
nodes <- data.frame(c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "D", "E", "N", "O", "P", "Q", "R"))
names(nodes) <- "name"
nodes$name = as.character(nodes$name)
links <- data.frame(matrix( 
  c(0,  2,  318.167, 
0,  3,  73.85, 
0,  4,  51.1262,
0,  5,  6.83333,
0,  6,  5.68571,
0,  7,  27.4167,
0,  8,  4.16667,
0,  9,  27.7381,
1,  10, 627.015,
1,  3,  884.428,
1,  4,  364.211,
1,  13, 12.33333,
1,  14, 9,
1,  15, 37.2833,
1,  16, 9.6,
1,  17, 30.5485), nrow=16, ncol=3, byrow = TRUE))
colnames(links) <- c("source", "target", "value")
links$source = as.integer(links$source)
links$target = as.integer(links$target)
links$value = as.numeric(links$value)
sankeyNetwork(Links = links, Nodes = nodes, Source = "source",
          Target = "target", Value = "value", NodeID = "name",
          fontSize = 12, fontFamily = 'Arial', nodeWidth = 20)

The problem is that A and B only have common links to D and E. Although the links are correctly displayed, D and E are also shown at the right-bottom. How can I avoid this ? Note: If I specify

nodes <- data.frame(c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "N", "O", "P", "Q", "R"))

no network at all is created.

enter image description here

like image 766
Florian Seliger Avatar asked Oct 25 '25 23:10

Florian Seliger


1 Answers

Nodes must be unique, see below example. I removed repeated nodes: "D" and "E", then in links, I removed links that reference to nodes that do not exist. We have only 16 nodes, zero based 0:15. And in your links dataframe, you have last 2 rows referencing to 16 and 17.


Or as @CJYetman (networkD3 author) comments:

Another way to say it... every node that is in the nodes data frame will be plotted, even if it has the same name as another node, because the index is technically the unique id.

library(networkD3)

nodes <- data.frame(name = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "N", "O", "P", "Q", "R"), 
                    ix = 0:15)

links <- data.frame(matrix( 
  c(0,  2,  318.167, 
    0,  3,  73.85, 
    0,  4,  51.1262,
    0,  5,  6.83333,
    0,  6,  5.68571,
    0,  7,  27.4167,
    0,  8,  4.16667,
    0,  9,  27.7381,
    1,  10, 627.015,
    1,  3,  884.428,
    1,  4,  364.211,
    1,  13, 12.33333,
    1,  14, 9,
    1,  15, 37.2833), nrow=14, ncol=3, byrow = TRUE))
colnames(links) <- c("source", "target", "value")

sankeyNetwork(Links = links, Nodes = nodes, Source = "source",
              Target = "target", Value = "value", NodeID = "name",
              fontSize = 12, fontFamily = 'Arial', nodeWidth = 20)

enter image description here

like image 136
zx8754 Avatar answered Oct 28 '25 15:10

zx8754



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!