forceNetwork不显示任何边缘 [英] forceNetwork not displaying any edges
问题描述
我试图找出对forceNetwork()的节点和链接数据帧的精确要求,因为我的网络显示的是彩色可移动点,而没有任何边缘将它们连接起来,我也不知道为什么.据我所知,要求是:
I am trying to figure out the EXACT requirements for the node and link dataframes in forceNetwork() because my network is displaying colored movable points without any edges connecting them and I dont know why. As far as I can tell the requirements are:
节点数据框必须具有:
nodes dataframe must have:
链接数据框必须具有:
links dataframe must have:
> head(links)
source target value
1 11170 7 1
2 2840 2 1
3 32595 2 1
4 45410 8 1
5 52720 12 1
6 61720 6 1
> head(nodes)
nodeID group
1 11170 2
2 2840 1
3 32595 2
4 45410 3
5 52720 1
6 61720 2
> head(E(g))
Edge sequence:
[1] 7 -- 11170
[2] 2 -- 2840
[3] 2 -- 32595
[4] 8 -- 45410
[5] 12 -- 52720
[6] 6 -- 61720
> head(V(g))
Vertex sequence:
[1] "11170" "2840" "32595" "45410" "52720" "61720"
> typeof(nodes$nodeID[1])
[1] "integer"
> typeof(links$source[1])
[1] "integer"
> dim(links)
[1] 121 3
> dim(nodes)
[1] 135 2
> forceNetwork(Links = links, Nodes = nodes,Source = "source", Target = "target", NodeID = "nodeID",Group = "group", opacity = 0.8, colourScale = "d3.scale.category10()")
推荐答案
Links
数据框中的source
和 target
向量必须为数字,并且其值引用它们表示的Nodes
数据框中的节点的索引(零索引,与R不同,因为它由JavaScript代码使用).
The source
and target
vectors in the Links
data frame must be numeric, and their values refer to the index of the node in the Nodes
data frame which they represent (zero-indexed, unlike R, because it's used by the JavaScript code).
Nodes
数据框的nodeID
矢量给出每个节点的名称(字符或数字),当您将鼠标悬停在结果可视化中的某个节点上时会显示该名称.
The nodeID
vector of the Nodes
data frame gives the name (character or numeric) of each node, which is displayed when you hover over a node in the resulting visualization.
考虑到您的示例数据,我怀疑您在links$source
中拥有的数据将成为节点的ID,而在links$target
中具有的数据就是其他东西.虽然它们都是数字矢量,但它们的值并不代表它们在Nodes
数据框中引用的节点的索引.
Given your example data, I suspect that the data you have in links$source
is meant to be the ID of the nodes, and the data you have in links$target
is something else. While they're both numeric vectors, the values do not represent the index of the node they refer to in the Nodes
data frame.
如果在links$source
和links$target
向量中具有节点名称/ID,而不是在Nodes
数据框中的节点索引,则可以像这样修复它...
If you have node names/IDs in your links$source
and links$target
vectors instead of the index of the nodes in your Nodes
data frame, you can fix it like so...
links <- read.table(header = T, text = "
source target value
11170 7 1
2840 2 1
32595 2 1
45410 8 1
52720 12 1
61720 6 1
")
nodes <- read.table(header = T, text = "
nodeID group
11170 2
2840 1
32595 2
45410 3
52720 1
61720 2
")
# build a new Nodes data frame that includes every
# unique node found in links$source and links$target
nodes_complete <- data.frame(nodeID = unique(c(links$source, links$target)))
# add groups already identified in your original Nodes data frame
nodes_complete$group <- nodes$group[match(nodes_complete$nodeID, nodes$nodeID)]
# convert your links$source and links$target nodes to their index
# in the new Nodes data frame (zero-indexed)
links$source <- match(links$source, nodes_complete$nodeID) - 1
links$target <- match(links$target, nodes_complete$nodeID) - 1
# now the forceNetwork function will run as expected
library(networkD3)
forceNetwork(Links = links, Nodes = nodes_complete, Source = "source",
Target = "target", NodeID = "nodeID", Group = "group",
opacity = 0.8,
colourScale = "d3.scaleOrdinal(d3.schemeCategory10);")
这篇关于forceNetwork不显示任何边缘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!