首页 > 解决方案 > SankeyNetwork...如何处理添加的数据

问题描述

这是物种分类的(完全随机)图的示例:

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)

但是,颜色无法正确匹配,我不知道如何正确设置它们......我想要的是“新”和“灭绝”以具有不同的颜色,而其他颜色则具有与其对应的匹配颜色类别。有任何想法吗?也许是一个不同的绘图软件允许我添加这个类别或者只是 2 个堆叠条,它们之间有流量,所有这些 Sankey-Network-Stuff 似乎都过度投资于我想要的东西......

我尝试过更明确地了解颜色(即列出所有节点)

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"  ])'

在这种情况下,只有“new 2021”是正确的,“unknown”是深灰色,其他都是黑色。

标签: rsankey-diagram

解决方案


您的答案中的颜色编码似乎无法识别,因此返回黑色。您可以使用 RGB 颜色编码来解决此问题。我不完全确定为什么无法识别颜色。sankeyNetwork 函数可能不支持它。请查看此函数的文档以获取更多信息。

### 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)

推荐阅读