Following is the code I used to create Sankey diagram -
links <- FinalFlow
# From these flows we need to create a node data frame: it lists every entities involved in the flow
nodes <- data.frame(
name=c(as.character(links$From),
as.character(links$To)) %>% unique()
)
# With networkD3, connection must be provided using id, not using real name like in the links dataframe.. So we need to reformat it.
links$IDsource <- match(links$From, nodes$name)-1
links$IDtarget <- match(links$To, nodes$name)-1
links$group <- as.factor(c(rep(c("T1", "T2", "T3"), 9)))
# Add a 'group' column to each node. Here I decide to put all of them in the same group to make them grey
nodes$group <- as.factor(c("my_unique_group"))
# Give a color for each group:
my_color <- 'd3.scaleOrdinal() .domain(["T1", "T2","T3", "my_unique_group"]) .range(["#69b3a2", "steelblue", "grey"])'
# Thus we can plot it
p <- sankeyNetwork(Links = links, Nodes = nodes, Source = "IDsource",
Target = "IDtarget", Value = "MaterialFlow", NodeID = "name",
units = "Ton", fontSize = 12, nodeWidth = 30)
p
I got a perfect diagram, but I could not add the value labels along the links and nodes. The values are visible when I hover the mouse around the links or nodes. Can anyone help me on how I can apply the value labels so that the labels are visible after exporting as an image file?
Thanks in advance, Kazi