我想使用R和D3js创建一个自由规模的网络。 我对a network like that one感兴趣。 在此示例中,使用的 Miserables 网络以JSON格式编写:
{
"nodes": [
{"id": "Myriel", "group": 1},
{"id": "Napoleon", "group": 1},
{"id": "Mlle.Baptistine", "group": 1},
{"id": "Mme.Magloire", "group": 1},
{"id": "CountessdeLo", "group": 1},
...
],
"links": [
{"source": "Napoleon", "target": "Myriel", "value": 1},
{"source": "Mlle.Baptistine", "target": "Myriel", "value": 8},
{"source": "Mme.Magloire", "target": "Myriel", "value": 10},
{"source": "Mme.Magloire", "target": "Mlle.Baptistine", "value": 6},
{"source": "CountessdeLo", "target": "Myriel", "value": 1},
...
]
}
我考虑过使用包sample_pa
的{{1}}函数在R中创建网络(有更好的功能吗?)。
这是我的代码:
igraph
由此产生的网络是:
nNodes <- 50 # number of nodes
p <- 1 # power of attachment (1 is linear)
nEdg <- 1 # number of edges added on each time step
dir <- F # undirected graph
ba <- sample_pa(n=nNodes, power=p, m=nEdg, directed=dir)
# Plot graph
plot(ba, vertex.size=8, vertex.label=NA)
# Print network
print(ba)
write_graph(ba, "ba.json", "edgelist")
我希望节点的编号为0 1
0 2
0 3
0 9
0 27
2 5
2 21
3 4
3 12
3 15
...
,id
没有值。
因此,我希望保留文件结构,如:
group
我该怎么做? 非常感谢你