rd3.jssankey-diagramhtmlwidgetsnetworkd3

Is it possible to change the link names in sankey plots from networkD3 R package


I have plotted this sankey plot using the networkD3 package in R but want to change the link names so that in each node the #number is removed. So essentially, I only want BV in each node without having to specify which number it is, and so on.

If I do remove the hashtags with its corresponding number in my excel sheet from which the links and nodes are read, the following sankey plot is all convoluted because the code cannot distinguish the order of events.

This is my R code:

library(networkD3)
    
sankeyNetwork(Links = links211, Nodes = nodes211,
              Source = "IDsource", Target = "IDtarget",
              Value = "value", NodeID = "name",
              sinksRight=T, fontSize = 17, iterations = 0,
              nodeWidth = 12.5, nodePadding = 15,
              LinkGroup = "group",
              colourScale = my_color,
              NodeGroup = "group")

This is my current sankey plot in which i would like to remove all the hashtags and numbers, without changing the order of events (links and nodes).


Solution

  • Assuming you have data that looks something like this...

    links211 <- read.csv(header = TRUE, strip.white = TRUE, text = "
    source, target,  value, group
    BV #1,  BV #2,   10,    t1
    BV #1,  SAV #2,  8,     t1
    BV #1,  ROSS #2, 4,     t1
    BV #2,  BV #3,   6,     t2
    BV #2,  SAV #3,  3,     t2
    BV #2,  ROSS #3, 2,     t2
    ")
    
    nodes211 <- data.frame(name = sort(unique(c(links211$source, links211$target))))
    nodes211$group  <- substr(nodes211$name, 1, 1)
    
    links211$IDsource <- match(links211$source, nodes211$name) - 1
    links211$IDtarget <- match(links211$target, nodes211$name) -1 
    
    links211
    #>   source  target value group IDsource IDtarget
    #> 1  BV #1   BV #2    10    t1        0        1
    #> 2  BV #1  SAV #2     8    t1        0        5
    #> 3  BV #1 ROSS #2     4    t1        0        3
    #> 4  BV #2   BV #3     6    t2        1        2
    #> 5  BV #2  SAV #3     3    t2        1        6
    #> 6  BV #2 ROSS #3     2    t2        1        4
    
    nodes211
    #>      name group
    #> 1   BV #1     B
    #> 2   BV #2     B
    #> 3   BV #3     B
    #> 4 ROSS #2     R
    #> 5 ROSS #3     R
    #> 6  SAV #2     S
    #> 7  SAV #3     S
    
    library(networkD3)
    
    sankeyNetwork(Links = links211, Nodes = nodes211,
                  Source = "IDsource", Target = "IDtarget",
                  Value = "value", NodeID = "name",
                  sinksRight=T, fontSize = 17, iterations = 0,
                  nodeWidth = 12.5, nodePadding = 15,
                  LinkGroup = "group",
                  # colourScale = my_color,
                  NodeGroup = "group"
                  )
    

    You can add a column to your nodes data frame with the text exactly as you want it to be displayed for each node (even if they are not unique) and pass the column name as the value to the NodeID argument.

    nodes211$display_name  <- sub(" #.$", "", nodes211$name)
    
    sankeyNetwork(Links = links211, Nodes = nodes211,
                  Source = "IDsource", Target = "IDtarget",
                  Value = "value", NodeID = "display_name",
                  sinksRight=T, fontSize = 17, iterations = 0,
                  nodeWidth = 12.5, nodePadding = 15,
                  LinkGroup = "group",
                  # colourScale = my_color,
                  NodeGroup = "group"
    )