我试图在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)
}
Run Code Online (Sandbox Code Playgroud)
目前的产量低于.

我的目标是将标签放在顶点图形内,并展开顶点的宽度以容纳标签.
这是一个例子.在一些肮脏的技巧(即将顶点大小乘以200)中,关键是使用两个绘图命令,这样我们就可以strwidth()在绘图尺寸设置为第一个之后测量标签的宽度(和高度)(空)情节.
library(igraph)
camp <- graph.formula(Harry:Steve:Don:Bert - Harry:Steve:Don:Bert,
Pam:Brazey:Carol:Pat - Pam:Brazey:Carol:Pat,
Holly - Carol:Pat:Pam:Jennie:Bill,
Bill - Pauline:Michael:Lee:Holly,
Pauline - Bill:Jennie:Ann,
Jennie - Holly:Michael:Lee:Ann:Pauline,
Michael - Bill:Jennie:Ann:Lee:John,
Ann - Michael:Jennie:Pauline,
Lee - Michael:Bill:Jennie,
Gery - Pat:Steve:Russ:John,
Russ - Steve:Bert:Gery:John,
John - Gery:Russ:Michael)
V(camp)$label <- V(camp)$name
set.seed(42) ## to make this reproducable
co <- layout.auto(camp)
plot(0, type="n", ann=FALSE, axes=FALSE, xlim=extendrange(co[,1]),
ylim=extendrange(co[,2]))
plot(camp, layout=co, rescale=FALSE, add=TRUE,
vertex.shape="rectangle",
vertex.size=(strwidth(V(camp)$label) + strwidth("oo")) * 100,
vertex.size2=strheight("I") * 2 * 100)
Run Code Online (Sandbox Code Playgroud)

顺便说一句.这对SVG图形效果不太好,因为无法测量R中文本的宽度,SVG设备只能猜测.