0

Here is an example of a (totally random) graph of a categorization of species:

stat.old<-c("new2021","casual_old","established_local_old","established_old","unknown_old","questionable_old")
stat.new<-c("casual","established_local","established","unknown","questionable", "extinct")

dummytest<-expand.grid(stat.old=stat.old,stat.new=stat.new)

### value defines how MANY species are in this specific "flow"
dummytest$value<-sample(c(0:50),36,replace=T)

dummynodes<-data.frame(name=c(as.character(dummytest$stat.old),  as.character(dummytest$stat.new)) %>% 
                         unique())

### use match() to find IDs for the nodes
dummytest$idsource<-match(dummytest$stat.old, dummynodes$name)-1
dummytest$idtarget<-match(dummytest$stat.new, dummynodes$name)-1

### then gsub the "_old" out, so that the nodes have the same name
dummynodes$name<-gsub("_old","", dummynodes$name)

### set colors
my_color <- 'd3.scaleOrdinal() .domain(["new2021","casual","established_local","established","unknown","questionable", "extinct"]) .range(["red","darkolivegreen2","darkolivegreen3","darkolivegreen4","gold3","darkgrey", "black ])'

### plot network
sankeyNetwork(Links=dummytest, Nodes=dummynodes, Source="idsource",Target="idtarget", Value="value", NodeID="name", sinksRight=F, fontSize=22, fontFamily="Calibri", colourScale=my_color)

However, the colors just won't match up properly and I have no idea how to set them properly... what I want is "new" and "extinct" to have a distinct color and the others to have matching colors to their corresponding categories. Any ideas? Maybe a different plotting software that allows me to add this category or just 2 stacked bars with the flows between them, all this Sankey-Network-Stuff seems overly invested for what I want...

I have tried being more explicit about the colors (i.e. listing all nodes)

my_color <- 'd3.scaleOrdinal() 
.domain(["new2021","casual","established_local","established","unknown","questionable","casual","established_local","established","unknown",           "questionable","extinct"]) 
.range(["red","darkolivegreen2","darkolivegreen3","darkolivegreen4","darkgrey","gold3","darkolivegreen2","darkolivegreen3","darkolivegreen4","gold3", "darkgrey","black"  ])'

In this case only "new 2021" turns up correct, "unknown" is darkgrey and all others turn out black.

Gmichael
  • 526
  • 1
  • 5
  • 16
  • 1
    Ah now I get it. the colourcoding is not recognized. you should use the RGB colourcoding. e.g. #a2cd5a is the same as DarkOliveGreen3. You can google the rest of the correct RGB colourcoding – Koot6133 Jul 29 '21 at 13:39
  • 1
    It works now, do you want to formalize your answer and collect some "fame"? Also, can you pls explain why this would make any difference, normally R colors are recognized (which can be shown in several examples)... – Gmichael Jul 29 '21 at 13:55
  • follow-up question: is there a function in R that converts a color name to an RGB code like yours, I can only find conversions to HEX codes and those aren't accepted... – Gmichael Jul 29 '21 at 13:59
  • regarding your follow up question. check this function: https://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/col2rgb.html – Koot6133 Aug 02 '21 at 07:51

1 Answers1

0

It seems that the colourcoding in your answer is not recognized and therefore returns black. You can solve the problem by using RGB colourcoding. I am not completely sure why the colours are not recognized. It is probably not supported by the sankeyNetwork function. Please look in the documentation of this function for more information.

### set colors --> Change colours to RGB codes. e.g. Olivegreen2 = #a2cd5a
my_color <- 'd3.scaleOrdinal() 
.domain(["new2021","casual","established_local","established","unknown","questionable","casual","established_local","established","unknown",           "questionable","extinct"]) 
.range(["red","#a2cd5a","darkolivegreen3","darkolivegreen4","darkgrey","gold3","darkolivegreen2","darkolivegreen3","darkolivegreen4","gold3", "darkgrey","black"  ])'
    
### plot network
sankeyNetwork(Links=dummytest, Nodes=dummynodes, Source="idsource",Target="idtarget", Value="value", NodeID="name", sinksRight=F, fontSize=22, fontFamily="Calibri", colourScale=my_color)
Koot6133
  • 1,428
  • 15
  • 26