问题
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()")
回答1:
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).
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.
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.
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);")
来源:https://stackoverflow.com/questions/30426708/forcenetwork-not-displaying-any-edges