如何为networkD3的sankeyNetwork中的组着色? [英] How to color groups in networkD3's sankeyNetwork?
问题描述
我的节点由名称和组组成,但是我似乎无法在sankey图中为组实现不同的颜色.颜色可以是默认的全蓝色,也可以是下面的代码全黑.
My nodes consists of names and groups yet I can't seem to implement distinct colors for groups in my sankey diagram. The colors are either all blue with defaults or all black using the code below.
这是我使用的代码:
sankeyNetwork(
Links = data$links,
Nodes = data$nodes,
Source= "source",
Target = "target",
Value = "weight",
NodeID = "names",
fontSize = 15,
NodeGroup = "group"
))
这是我得到的输出:
Here's the output I'm getting:
推荐答案
Nodes
数据框中的NodeGroup
向量必须是非数字的.从文档中看不出来.因为您没有提供正在使用的数据,所以我们无法确定这是否是您遇到的问题,但是在@ john-friel提出的示例中,这就是问题所在.这是一个有效的示例,唯一的变化是group
向量被强制转换为字符向量...
The NodeGroup
vector in the Nodes
data frame needs to be non-numeric. That's not obvious from the documentation. Because you didn't provide the data you're working with, we can't be sure if that is the problem you were having, but in the example that @john-friel made, that is the problem. Here's a working example with the only change being that the group
vector is coerced to a character vector...
library(networkD3)
source <- c(0,1,2,3,4,5)
target <- c(2,2,2,3,1,0)
value <- c(33,44,55,66,77,88)
sankeydata <- data.frame(source,target, value)
names <- c('a', 'b', 'c', 'd', 'e', 'f')
id <- c(0,1,2,3,4,5)
group <- as.character(c(1,1,1,2,2,2)) # this is the only line changed
sankeyNodes <- data.frame(names,id, group)
sankeyNetwork(Links = sankeydata, Nodes = sankeyNodes, Source = "source",
Target = "target", Value = "value", NodeID = "names",
NodeGroup = "group", fontSize = 12, nodeWidth = 30)
这篇关于如何为networkD3的sankeyNetwork中的组着色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!