将顶点大小与igraph中的标签大小匹配 [英] Match vertex size to label size in igraph

查看:338
本文介绍了将顶点大小与igraph中的标签大小匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在R中使用igraph绘制小型网络.网络中的每个顶点都有一个名称,该名称与其标签等效.我想使每个顶点都有一个矩形符号,该符号正好适合其标签.

I am trying to plot small networks using igraph in R. Each vertex in the network has a name, which is equivalent to its label. I would like to make each vertex have a rectangular symbol that is just large enough to fit its label.

这是我的主要灵感.

使用igraph的最佳方法是什么?

What is the best way to do this with 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)
}

当前输出如下.

我的目标是将标签放置在顶点图形的内部,并扩展顶点的宽度以容纳标签.

My goal is to place the labels inside of the vertex graphic, and expand the width of the vertex to accommodate the label.

推荐答案

这里是一个示例.在一些肮脏的技巧中(例如,将顶点大小乘以200),关键是使用两个绘图命令,以便在使用设置了绘图大小之后,我们可以使用strwidth()测量标签的宽度(和高度).第一个(空)图.

Here is an example. Among some dirty tricks (i.e. multiplying the vertex size by 200), the key is to use two plot commands, so that we can measure the width (and height) of the labels with strwidth(), after the plot size is set with the first (empty) plot.

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)

顺便说一句.这实际上不适用于SVG图形,因为无法测量R中文本的宽度,因此SVG设备只能做出猜测.

Btw. this does not really work well with SVG graphics, because there is no way to measure the width of the text from R, the SVG device only makes guesses.

这篇关于将顶点大小与igraph中的标签大小匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆