2

I am using the R package networkD3 to plot the following Sankey diagram. The values set the size of the links. I need a function who adds the same value as text inside the nodes.

# Library
library(networkD3)
library(dplyr)

# Make a connection data frame
links <- data.frame(
  source=c("group_A","group_A", "group_B", "group_C", "group_C", "group_E"), 
  target=c("group_C","group_D", "group_E", "group_F", "group_G", "group_H"), 
  value=c(2,3, 2, 3, 1, 3)
)

# 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$source), as.character(links$target)) %>% 
    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$source, nodes$name)-1 
links$IDtarget <- match(links$target, nodes$name)-1

# Add a 'group' column to each connection:
links$group <- as.factor(c("type_a","type_a","type_a","type_b","type_b","type_b"))

# 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(["type_a", "type_b", "my_unique_group"]) .range(["#69b3a2", "steelblue", "grey"])'

# Make the Network
p <- sankeyNetwork(Links = links, Nodes = nodes, Source = "IDsource", Target = "IDtarget", 
                   Value = "value", NodeID = "name", 
                   colourScale=my_color, LinkGroup="group", NodeGroup="group")

p

The code above shows the code structure of the sankey diagram as described also in the following link: https://www.r-graph-gallery.com/322-custom-colours-in-sankey-diagram.html

  • The value is shown in a tool-tip when you hover over the node or link. – CJ Yetman Aug 13 '19 at 07:42
  • This is right! I need to export the diagram as image. Therefore I need the values inside the diagram. – JessicaRjes Aug 13 '19 at 07:51
  • There is no built-in functionality for that. You will have to either modify the underlying JavaScript in the package, or inject custom JavaScript that will determine the size of the text for each node, modify the size of each node, create a SVG text element for each node, and determine/set the appropriate location of the text element based on the location of each node. – CJ Yetman Aug 13 '19 at 08:03
  • Can you give an code example for that? – JessicaRjes Aug 13 '19 at 08:16
  • No, not specifically. That’s a rather involved, specific task that I have not done before. – CJ Yetman Aug 13 '19 at 08:18
  • Have you maybe looked into `ggalluvial`? And then use `theme_void()` to get the Sankey diagram (without the frequency count on the `y` axis) – csgroen Aug 13 '19 at 08:25
  • thanks I will check this later.Regarding the networkD3 package and its Sankey application: I have an idea but no clue how to apply that. How can I add the value automatically behind each element in the source group. for example ```R ...source=c("group_A" + "value [1]","group_A"+"value[2]", ...), ...``` – JessicaRjes Aug 13 '19 at 08:53
  • You could do something like `links$source <- paste(links$source, "-", links$value)` but that won't really work because what you see in the plot is the nodes' label, not the links' label. Keep in mind, one node can have more than one link leaving/entering it, so the "value" of the node should be the sum of the values of the links that leave or enter it. – CJ Yetman Aug 13 '19 at 13:10

0 Answers0