0

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

  • 1
    Does this answer your question? [In R, how to display value on the links/paths of Sankey Graph?](https://stackoverflow.com/questions/68266728/in-r-how-to-display-value-on-the-links-paths-of-sankey-graph) – CJ Yetman Nov 03 '21 at 12:53
  • @CJYetman, thank you so much! The solution you shared allow me to put label in the link. Still, I need to solve two things - 1) How can I control the front size of the labels? 2) How can I add value label inside the nodes too? – Kazi Masel Nov 03 '21 at 16:31

0 Answers0