我有一个简单的sankey图,使用networkD3
包在闪亮的应用程序中生成。如何对source
和/或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包来生成允许对节点进行排序的sankey图?如果是的话,怎么做?
答案 0 :(得分:9)
在iterations = 0
内设置sankeyNetwork()
就可以了。现在,节点的绘制顺序与节点数据框中的顺序相同。
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)