如何在闪亮的应用程序中对桑基图中的源 and/or 目标节点进行排序?

How to sort source and/or target nodes in a sankey diagram within a shiny app?

我有一个简单的 sankey 图,它是在闪亮的应用程序中使用 networkD3 包生成的。 source and/or target 个节点如何排序?

正如您在 MWE 中看到的,默认情况下,源节点(A、B、C、D、E)和目标节点(V、W、X、Y、Z)都不会被排序。至少,排序对我来说是无法理解的。

代码:

library("shiny")
library("networkD3")

ui <- fluidPage(
  column(3),
  column(6, sankeyNetworkOutput("mySankeyD")),
  column(3)
)

server <- function(input, output) {
  output$mySankeyD <- renderSankeyNetwork({
    myDf <- list(
      nodes=data.frame(name=c( "A", "B", "C", "D", "E",
                               "V", "W", "X", "Y", "Z")),
      links=data.frame(source=as.integer(c(0, 1, 2, 3, 3, 4, 4)),
                       target=as.integer(c(7, 6, 7, 8, 7, 5, 9)),
                       value =           c(1, 4, 1, 5, 1, 5, 3)
      )
    )

    sankeyNetwork(Links = myDf$links, Nodes = myDf$nodes, Source = "source",
                  Target = "target", Value = "value", NodeID = "name",
                  units = "TWh", fontSize = 25, nodeWidth = 30, fontFamily = "sans-serif", iterations = 30)
  })
}

shinyApp(ui, server)

sankeyNetwork()如何确定节点的顺序?是否可以按字母顺序对它们进行排序?

我希望对源节点进行排序,如果可能的话还对目标节点进行排序。

编辑 正如 @emilliman5 在评论中指出的那样,无法手动对节点进行排序。

因此,是否有其他 R 包可以生成允许对节点进行排序的桑基图?如果是,如何操作?

sankeyNetwork() 中设置 iterations = 0 就成功了。现在节点的绘制顺序与节点数据框中的顺序相同。

library("networkD3")

myDf <- list(
    nodes=data.frame(name=c( "A", "B", "C", "D", "E",
                             "V", "W", "X", "Y", "Z")),
    links=data.frame(source=as.integer(c(0, 1, 2, 3, 3, 4, 4)),
                     target=as.integer(c(7, 6, 7, 8, 7, 5, 9)),
                     value =           c(1, 4, 1, 5, 1, 5, 3)
    )
)

sankeyNetwork(Links = myDf$links, Nodes = myDf$nodes, Source = "source",
              Target = "target", Value = "value", NodeID = "name",
              units = "TWh", fontSize = 25, nodeWidth = 30, 
              fontFamily = "sans-serif", iterations = 0)