D3 Sankey Diagram: Changing Node Position

Viewed 386

I am creating a Sankey diagram in Shiny using networkD3 library. I need to change the position of one node (rotate it on 90d and move down). For this, I use js as in the little example below. However, after changing the node I need to update the link and I have no idea how to do that.

library(shiny)
library(networkD3)
library(htmlwidgets)
library(tibble)
library(dplyr)
library(ggplot2)
library(ggforce)


ui <- fluidPage(
  fluidRow(sankeyNetworkOutput("plot"))
)

server <- function(input, output, session) {
  session$onSessionEnded(stopApp)

  URL <- paste0(
    "https://cdn.rawgit.com/christophergandrud/networkD3/",
    "master/JSONdata/energy.json"
  )
  energy <- jsonlite::fromJSON(URL)
  
  output$plot <- renderSankeyNetwork({
    sn <- sankeyNetwork(
      Links = energy$links, Nodes = energy$nodes, Source = "source",
      Target = "target", Value = "value", NodeID = "name",
      units = "TWh", fontSize = 12, nodeWidth = 30, nodePadding = 0,
      width = "100%", sinksRight = FALSE
    )

    update_diagr <-
      'function(el, x) {
          d3.select(el)
          .selectAll(".node rect")
          .filter(function(d) { return d.name.startsWith("National"); })
          .attr("transform", "translate(0 100) rotate(90)");
          
        }'
    onRender(sn, update_diagr)
  })
}

shinyApp(ui, server)

enter image description here

1 Answers
Related