简体   繁体   中英

d3Network: How can I relate nodes to links?

I'm playing around with the d3Network package. I've run the following example from the help file. I'm sure it's very simple, but I can't figure out how it identifies what node to assign to what link.

devtools::install_github("d3Network", "christophergandrud")
library(d3Network)
library(rjson)

json_file <- "https://raw.github.com/christophergandrud/d3Network/master/JSONdata/miserables.json"
json_text <- paste(readLines(json_file), collapse = '')
MisLinks <- JSONtoDF(json_text, array = "links")
MisNodes <- JSONtoDF(json_text, array = "nodes")

d3ForceNetwork(Links = MisLinks, 
           Nodes = MisNodes, 
           Source = "source",
           Target = "target", 
           Value = "value", 
           NodeID = "name",
           Group = "group", 
           opacity = 1,
           file = "example_graph_force_directed.html")

Looking at the two data frames. The documentation says the Links data frame has to be ordered by source and the node names are in the same order.

Does this mean 'Node 1' is 'Myriel' and 'Node 2' is 'Napoleon'? If so, then where does Node 0 get named?

> head(MisLinks)
  source target value
1      1      0     1
2      2      0     8
3      3      0    10
4      3      2     6
5      4      0     1
6      5      0     1


> head(MisNodes)
             name group
1          Myriel     1
2        Napoleon     1
3 Mlle.Baptistine     1
4    Mme.Magloire     1
5    CountessdeLo     1
6        Geborand     1

Would anyone be able to explain, or show me a smaller example that might explain things?

Thanks

here is a simple example, created purely in R:

jjnames<-c("A","B","C","D","E","F","G","H")
mygroup<-c(1,1,1,2,2,2,2,2)
JJNodes<-data.frame( name=jjnames, group=mygroup )
jjsources<-c( 0 , 4 )
jjtargets<-c(  1,  5 )
jjvalues<-rep( c(1,2) , length(jjtargets) ) ; jjvalues<-jjvalues[1:length(jjtargets)]
JJLinks<-data.frame(source=jjsources,target=jjtargets,value=jjvalues)
d3ForceNetwork(Links = JJLinks, Nodes = JJNodes, 
           Source = "source", Target = "target", 
           Value = "value", NodeID = "name", 
           Group = "group", width = 550, height = 400, zoom=TRUE,
           opacity = 0.9)`

the links are defined by pairs of node indices. 0 => 1 links the first node with the second node.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM