我正在尝试使用R中的igraph绘制小型网络。网络中的每个顶点都有一个名称,相当于它的标签。我想让每个顶点都有一个矩形符号,大小刚好能容纳它的标签。
这是我的主要灵感来源。
如何使用igraph最佳实践?
编辑:更多信息
代码在 这里
jsonToNM <- function(jfile, directed=TRUE) {
# Requires "rjson" and "igraph"
nm.json <- fromJSON(file=jfile)
nm.graph <- c()
# Initialize the graph with the given nodes
g <- graph.empty(n=length(nm.json), directed=directed)
# Add their names
V(g)$name <- names(nm.json)
V(g)$label <- V(g)$name
# Now, add the edges
for(i in 1:length(nm.json)) {
# If the node has a "connected" field,
# then we note the connections by looking
# the names up.
if(length(nm.json[[i]]$connected > 0)) {
for(j in 1:length(nm.json[[i]]$connected)) {
# Add the entry
g <- g + edge(names(nm.json)[i],
nm.json[[i]]$connected[j])
}
}
}
plot(g, vertex.label.dist=1.5)
}
当前输出如下。
dput
来提供数据。 - David Robinsonjfile
。 - Gabor Csardi