r - How to sort source and/or target nodes in a sankey diagram within a shiny app? -
i have simple sankey diagram, generated using networkd3
package inside shiny app. how can source
and/or target
nodes sorted?
as can see in mwe, default, neither source nodes (a, b, c, d, e) nor target nodes (v, w, x, y, z) sorted. @ least, sorting not comprehensible me.
code:
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)
how sankeynetwork()
determine order of nodes? possible sort them alphabetically?
i'd have sorted source nodes, if possible sorted target nodes.
edit @emilliman5 pointed out in comment, no possible sort nodes manually.
thus, there other r packages out there generate sankey diagrams allow sorting of nodes? if yes, how so?
setting iterations = 0
inside sankeynetwork()
did trick. nodes plotted same order in nodes dataframe.
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)
Comments
Post a Comment