2

在 forcenetwork 中,我找到了组和色标来为网络中的节点着色。我想要 2 个不同的组(根据用户在 2 个不同变量之间的输入将颜色应用于节点)。有可能吗?如果是,如何?

Forcenetwork - https://www.rdocumentation.org/packages/networkD3/versions/0.4/topics/forceNetwork

任何形式的帮助都会很有用。谢谢!

4

1 回答 1

1

colourScale参数确定调色板,而该group参数确定节点数据框中的向量名称,其中包含用于区分每个节点组的值。networkD3自动为数据中的每个不同组从调色板中选择一种唯一颜色,并将该颜色应用于该组中的每个节点。

library(networkD3)

links <- read.table(header = T, text = "
source  target  value
0       1       1
1       2       1
2       0       1
0       3       1
3       4       1
4       5       1
5       3       1
")

nodes <- read.table(header = T, text = "
name    group
zero    1
one     1
two     1
three   2
four    2
five    2
")

forceNetwork(Links = links, Nodes = nodes, 
             Source = "source", Target = "target", Value = "value", 
             NodeID = "name", Group = "group", 
             colourScale = JS("d3.scaleOrdinal(d3.schemeCategory10);"))

如果您的数据中有两个单独的变量共同确定每个节点的组,您应该在将它们传递给forceNetwork()...之前将它们组合到您的数据中。

nodes <- read.table(header = T, text = "
name    group1  group2
zero    A       D
one     B       E
two     C       F
three   A       E
four    B       F
five    C       D
")

nodes$group <- paste(nodes$group1, nodes$group2, sep = "_")

forceNetwork(Links = links, Nodes = nodes, 
             Source = "source", Target = "target", Value = "value", 
             NodeID = "name", Group = "group", 
             colourScale = JS("d3.scaleOrdinal(d3.schemeCategory10);"))
于 2017-09-19T12:11:05.970 回答