Displaying edge information in Sankey tooltip

前端 未结 1 544

I\'ve am using sankeyNetwork in the networkD3 package to create a visualisation

I would like to assign a name/ID to each edge, so that is appears in the tooltip. Ca

1条回答
  •  盖世英雄少女心
    2021-01-07 05:45

    This is not technically supported, but you can achieve it like this...

    library(networkD3)
    library(htmlwidgets)
    
    links <- data.frame(
      src = c(0, 0, 1, 2),
      target = c(2, 3, 2, 4),
      value = 1,
      name = c("first", "second", "third", "fourth")
    )
    
    nodes <- data.frame(name = c("one", "two", "three", "four", "five"))
    
    # save the result of sankeyNetwork in an object
    sn <- networkD3::sankeyNetwork(
      Links = links,
      Nodes = nodes,
      Source = 'src',
      Target = 'target',
      Value = 'value',
      NodeID = 'name'
    )
    
    # add the names back into the links data because sankeyNetwork strips it out
    sn$x$links$name <- links$name
    
    # add onRender JavaScript to set the title to the value of 'name' for each link
    sn <- htmlwidgets::onRender(
      sn,
      '
      function(el, x) {
      d3.selectAll(".link").select("title foreignObject body pre")
      .text(function(d) { return d.name; });
      }
      '
    )
    
    # display the result
    sn
    

    0 讨论(0)
提交回复
热议问题