这是一个例子。在一些肮脏的技巧(即将顶点大小乘以 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)
![igraph vertex label width](https://i.stack.imgur.com/zUxw9.png)
顺便提一句。这对于 SVG 图形来说并不能很好地工作,因为无法测量 R 中文本的宽度,SVG 设备只能进行猜测。