4

我的节点由名称和组组成,但我似乎无法在我的 sankey 图中为组实现不同的颜色。颜色要么是默认的全蓝色,要么是使用下面的代码全黑。

这是我使用的代码:

sankeyNetwork(        
Links = data$links,
Nodes = data$nodes,
Source= "source",
Target = "target",
Value = "weight",
NodeID = "names",
fontSize = 15,
NodeGroup = "group" 
))

这是我得到的输出: 在此处输入图像描述

4

2 回答 2

3

数据框中的NodeGroup向量Nodes必须是非数字的。这在文档中并不明显。因为您没有提供您正在使用的数据,我们无法确定这是否是您遇到的问题,但在 @john-friel 所做的示例中,这就是问题所在。这是一个工作示例,唯一的变化是group向量被强制转换为字符向量......

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)

在此处输入图像描述

于 2017-04-16T09:51:08.133 回答
0
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 <- c(1,1,1,2,2,2)

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)

我希望有两种颜色(因为有两组),但没有颜色返回。我和OP有同样的问题。

帮助文本建议 NodeGroup 负责颜色。

如果您为 library(networkD3) 中的另一个图形运行类似的代码:

#same data
forceNetwork(Links = sankeydata, Nodes = sankeyNodes , Source = "source",
         Target = "target", Value = "value", NodeID = "names",
         Group = "group", opacity = 0.8, zoom = TRUE)

在网络图中绘制两种不同的颜色。

于 2016-08-29T14:18:56.050 回答