-1

I am working on a app to display a sankey graph, using sankeyNetwork() from the 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:

  1. 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

  2. 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

Ali Fradi
  • 110
  • 1
  • 10
  • 1
    Welcome to StackOverflow. Please take some time to **re-organize** your questions and you can refer to "[What topics can I ask about here?](https://stackoverflow.com/help/on-topic)" and [how to ask good questions](https://stackoverflow.com/help/how-to-ask). – Hearen Jul 18 '18 at 06:12
  • Hey there! thank you but and you please report to me the points that I need to improve in my questions? – Ali Fradi Jul 18 '18 at 06:33
  • 1. You asked 3 questions. You should only ask one specific question. 2. This is basically a duplicate of the question you asked a few days ago. 3. You should minimize your code to demonstrate one specific problem – CJ Yetman Jul 18 '18 at 06:55
  • I'm very thankful for your advices though I have modified the question! excuse my eager to solve my problem mainly because I'm a beginner user of this website and developer of shiny app, how it looks now? – Ali Fradi Jul 18 '18 at 07:11
  • If you're asking a question about how to control the colors in the output of `sankeyNetwork()`, then all of the `shiny` related code is irrelevant. Maybe you should make a minimal reproducible example using only the `sankeyNetwork()` related code so that someone can help you understand how to control the colors, and once you understand that, maybe you will be able to include that output in `shiny` or anything else on your own. – CJ Yetman Jul 18 '18 at 08:54

1 Answers1

0

Here is a minimal reproducible example using some of your data and code that demonstrates how you can adjust the colors when using sankeyNetwork() from .

library(networkD3)

links <- read.csv2(header = TRUE, as.is = TRUE, text = "
source;target;value;lgroup
0;1;614;type_a
0;2;14;type_a
1;3;28;type_b
1;4;18;type_b
2;3;10;type_b
2;4;12;type_b
")

nodes <- read.csv2(header = TRUE, as.is = TRUE, text = "
name;ngroup
Mosaique FM;my_unique_group
Jawhara FM;my_unique_group
Shems FM;my_unique_group
Panorama FM;my_unique_group
IFM;my_unique_group
")

colorJS <- 'd3.scaleOrdinal().domain(["type_a", "type_b", "my_unique_group"])
              .range(["blue", "pink", "grey"])'

sankeyNetwork(Links = links, Nodes = nodes, Source = 'source', 
              Target = 'target', Value = 'value', NodeID = 'name',
              NodeGroup = 'ngroup', LinkGroup = 'lgroup', colourScale = colorJS)

enter image description here

CJ Yetman
  • 8,373
  • 2
  • 24
  • 56
  • In fact I tried this one, but whenever I insert colors and "paste" them to colorJS it causes me problem or even show a graph with white links and black nodes – Ali Fradi Jul 18 '18 at 12:23
  • You’ll have to provide a minimal reproducible example then. – CJ Yetman Jul 18 '18 at 12:29
  • Probably my main problem is how to get the character chain colorJS from the column lgroup and ngroup – Ali Fradi Jul 18 '18 at 12:47
  • Why do you refuse to provide a minimal reproducible example? Do you really expect me or someone else to spend valuable time guessing at what you really need or want, creating our own minimal reproducible example to demonstrate how to deal with what we think your problem might be, and then wait for your response to tell us that it's not really what you need and then start the process all over again... and for no compensation? – CJ Yetman Jul 18 '18 at 15:42