I am working on a shiny app to display a sankey graph, using sankeyNetwork()
from the networkd3 package, based on data input from CSV files (like those included in this post).
I'm having trouble coloring links in the sankey graph by group.
Here is the code I used:
output$splot <- renderSankeyNetwork({
print(names(data()))
sankeyNetwork(
Links = data(),
Nodes = label(),
Source = 'source',
Target = 'target',
Value = 'value',
NodeID = "name",
fontSize = input$x,
nodeWidth =0.6*input$x,
NodeGroup = "ngroup", LinkGroup = "lgroup",
iterations = 0
)
})
I wonder how can I use colorScale in my case so that I can modify colors cause all my tentatives led me to errors whether it displays nothing od white links and black nodes when I inspired the charachter to affect to colorScale from this
# Add a 'group' column to each connection:
links$group=as.factor(c("type_a","type_a","type_a","type_b","type_b","type_b"))
# Add a 'group' column to each node. Here I decide to put all of them in the same group to make them grey
nodes$group=as.factor(c("my_unique_group"))
# Give a color for each group:
my_color <- 'd3.scaleOrdinal() .domain(["type_a", "type_b", "my_unique_group"]) .range(["blue", "pink", "grey"])'
# Make the Network
sankeyNetwork(Links = links, Nodes = nodes, Source = "IDsource", Target = "IDtarget", Value = "value", NodeID = "name", colourScale=my_color, LinkGroup="group", NodeGroup="group")
Here are my CSV data:
Data:
source;target;value;lgroup 0;9;614;blue 0;10;14;pink 0;11;28;yellow 0;12;18;orange 0;13;10;red 0;14;12;green 1;9;512;blue 1;10;12;pink 1;11;10;yellow 1;12;8;orange 1;13;4;red 1;14;6;green 2;9;313;blue 2;10;13;pink 2;11;9;yellow 2;12;4;orange 2;13;3;red 2;14;3;green 3;9;48;blue 3;10;12;pink 3;11;1;yellow 3;12;1;orange 3;13;1;red 3;14;1;green 4;9;49;blue 4;10;8;pink 4;11;1;yellow 4;12;1;orange 4;13;1;red 4;14;1;green 5;9;37;blue 5;10;1;pink 5;11;1;yellow 5;12;1;orange 5;13;1;red 5;14;1;green 6;9;27;blue 6;10;1;pink 6;11;1;yellow 6;12;1;orange 6;13;1;red 6;14;1;green 7;9;23;blue 7;10;1;pink 7;11;1;yellow 7;12;1;orange 7;13;1;red 7;14;1;green 8;9;4;blue 8;10;1;pink 8;11;1;yellow 8;12;1;orange 8;13;1;red 8;14;1;green
Labels
name;ngroup Mosaique FM;violet Jawhara FM;violet Shems FM;violet Panorama FM;violet IFM;violet Diwan FM;violet Cap FM;violet Express FM;violet Knooz FM;violet Like;blue Love;pink Haha;yellow Sad;orange Angry;red Wow;green