#Tawkir_ahmed_code
#Sankey_plot
library(networkD3)
## create a dataframe with 10 nodes
nodes = data.frame("name" = c("Node_0", "Node_1", "Node_2", "Node_3", "Node_4", "Node_5",
"Node_6", "Node_7", "Node_8", "Node_9"))
## create edges with weights
links = as.data.frame(matrix(c(0, 5, 2, # node 0 -> node 5 with weight 2
0, 9, 2, # node 0 -> node 9 with weight 2
0, 6, 2, # node 0 -> node 5 with weight 2
1, 6, 1, # node 1 -> node 6 with weight 1
1, 7, 3, # node 1 -> node 7 with weight 3
1, 8, 2, # node 1 -> node 8 with weight 2
2, 9, 3, # node 2 -> node 9 with weight 3
3, 5, 1, # node 3 -> node 5 with weight 1
3, 8, 1, # node 3 -> node 8 with weight 1
3, 9, 5, # node 3 -> node 9 with weight 5
4, 9, 2, # node 4 -> node 9 with weight 2
4, 6, 2 # node 4 -> node 6 with weight 2
), byrow = TRUE, ncol = 3))
## set column names for links
names(links) = c("source", "target", "value")
## add edge types for coloring purpose
links$group = c("type_0",
"type_0",
"type_0",
"type_1",
"type_1",
"type_1",
"type_2",
"type_3",
"type_3",
"type_3",
"type_4",
"type_4")
## Create custom color list using d3 for each node
node_color <- 'd3.scaleOrdinal() .domain(["Node_0", "Node_1", "Node_2", "Node_3", "Node_4",
"Node_5", "Node_6", "Node_7", "Node_8", "Node_9", "type_0", "type_1", "type_2",
"type_3", "type_4"]) .range(["#bf5b17", "#beaed4", "#fdc086" , "#386cb0", "#7fc97f",
"#bf5b17", "#beaed4", "#fdc086" , "#386cb0", "#7fc97f", "#bf5b17", "#beaed4", "#fdc086" , "#386cb0", "#7fc97f"])'
## Draw Sankey Diagram
p = sankeyNetwork(Links = links, Nodes = nodes,
Source = "source", Target = "target",
Value = "value", NodeID = "name",
fontSize = 16, nodeWidth = 40,
colourScale = node_color,
LinkGroup="group")
p
0 comments:
Post a Comment