简体   繁体   English

D3 桑基图:更改节点 Position

[英]D3 Sankey Diagram: Changing Node Position

I am creating a Sankey diagram in Shiny using.networkD3 library.我正在使用 .networkD3 库在 Shiny 中创建一个 Sankey 图。 I need to change the position of one node (rotate it on 90d and move down).我需要更改一个节点的 position(将其旋转 90d 并向下移动)。 For this, I use js as in the little example below.为此,我在下面的小示例中使用了 js。 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)

在此处输入图像描述

You can try Plotly for alternative.您可以尝试拨打 Plotly 作为替代。 You can try specifying X,Y coordinates of each node to position them.您可以尝试将每个节点的 X,Y 坐标指定为 position 它们。 This way links won't disappear.这样链接就不会消失。 Documentation: https://plotly.com/r/sankey-diagram/#define-node-position文档: https://plotly.com/r/sankey-diagram/#define-node-position

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM