【问题标题】:Vertex Labels in igraph Rigraph R中的顶点标签
【发布时间】:2017-10-19 08:57:21
【问题描述】:

我正在使用 igraph 绘制一个非定向力网络。

我有一个nodeslinks 的数据框如下:

> links
  source target  value sourceID targetID
1      3      4 0.6245  1450552  1519842
2      6      8 0.5723  2607133  3051992
3      9      7 0.7150  3101536  3025831
4      0      1 0.7695   401517   425784
5      2      5 0.5535  1045501  2258363

> nodes
      name group size
1   401517     1    8
2   425784     1    8
3  1045501     1    8
4  1450552     1    8
5  1519842     1    8
6  2258363     1    8
7  2607133     1    8
8  3025831     1    8
9  3051992     1    8
10 3101536     1    8

我使用igraph 绘制如下:

gg <- graph.data.frame(links,directed=FALSE)
plot(gg, vertex.color = 'lightblue', edge.label=links$value, vertex.size=1, edge.color="darkgreen", 
     vertex.label.font=1, edge.label.font =1, edge.label.cex = 1, 
     vertex.label.cex = 2 )

在此图上,igraph 使用了 sourcetarget 的代理索引作为顶点标签。

我想使用真实 ID,在我的 links 表中表示为 sourceIDtargetID

所以,对于:

  source target  value sourceID targetID
1      3      4 0.6245  1450552  1519842

这将显示为:

(1450552) ----- 0.6245 ----- (1519842)

代替:

      (3) ----- 0.6245 ----- (4)

(请注意,代理索引在 links 数据帧中索引为零,在 nodes 数据帧中索引为 1。igraph 绘图需要此偏移量 1)。

我知道我需要以某种方式将 matchmap 代理索引指向 nodes 数据框中相应的 name。但是,我不知所措,因为我不知道 igraph 绘制标签的顺序。

我怎样才能做到这一点?

我已经咨询了以下问题,但无济于事:

Vertex Labels in igraph with R how to specify the labels of vertices in R R igraph rename vertices

【问题讨论】:

    标签: r plot igraph


    【解决方案1】:

    您可以像这样指定标签:

     library(igraph)
    gg <- graph.data.frame(
      links,directed=FALSE, 
      vertices = rbind(
        setNames(links[,c(1,4)],c("id","label")), 
        setNames(links[,c(2,5)], c("id","label"))))
    plot(gg, vertex.color = 'lightblue', edge.label=links$value, 
         vertex.size=1, edge.color="darkgreen", 
         vertex.label.font=1, edge.label.font =1, edge.label.cex = 1, 
         vertex.label.cex = 2 )
    

    你也可以通过

    merge(rbind(
        setNames(links[,c(1,4)],c("id","label")), 
        setNames(links[,c(2,5)], c("id","label"))), 
        nodes, 
        by.x="label", by.y="name")
    

    如果您需要其他节点属性,请添加到顶点参数。

    数据:

     links <- read.table(header=T, text="
      source target  value sourceID targetID
    1      3      4 0.6245  1450552  1519842
    2      6      8 0.5723  2607133  3051992
    3      9      7 0.7150  3101536  3025831
    4      0      1 0.7695   401517   425784
    5      2      5 0.5535  1045501  2258363")
    
     nodes <- read.table(header=T, text="
          name group size
    1   401517     1    8
    2   425784     1    8
    3  1045501     1    8
    4  1450552     1    8
    5  1519842     1    8
    6  2258363     1    8
    7  2607133     1    8
    8  3025831     1    8
    9  3051992     1    8
    10 3101536     1    8")
    

    【讨论】:

    • 感谢您的回答。真的很有用。当您说you could pass ... to the vertices argument 时,您到底是什么意思?你的意思是设置vertex.attr = merge....
    • 我的意思是graph.data.frame(..., vertices = merge(...))
    • 非常好,非常感谢。
    • 我接受了您的回答,因为尽管我使用了不同的方法,但我相信您的方法具有更广泛的用例并提供更多信息优势。再次感谢,祝你有美好的一天:)
    【解决方案2】:

    看来我能够重新利用这个问题的答案来实现这一目标。

    r igraph - how to add labels to vertices based on vertex id

    关键是在plot() 中使用vertex.label 属性并选择nodes$names 的切片子集。

    对于我们的索引,我们可以自动使用在igraph 中返回的有序默认标签。要提取这些,您可以输入V(gg)$names

    plot(gg) 内我们可以写:

    vertex.label = nodes[c(as.numeric(V(gg)$name)+1),]$name
    # 1 Convert to numeric
    # 2 Add 1 for offset between proxy links index and nodes index
    # 3 Select subset of nodes with above as row index. Return name column
    

    完整代码:

    gg <- graph.data.frame(links,directed=FALSE)
    plot(gg, vertex.color = 'lightblue', edge.label=links$value, vertex.size=1, edge.color="darkgreen", 
         vertex.label.font=1, edge.label.font =1, edge.label.cex = 1, 
         vertex.label.cex = 2, vertex.label = nodes[c(as.numeric(V(gg)$name)+1),]$name)
    

    根据上面的数据,这给出了:

    【讨论】:

      【解决方案3】:

      最简单的解决方案是重新排序links 的列,因为根据文档:

      "如果 vertices 为 NULL,则 d 的前两列用作符号边列表,附加列用作边属性。"

      因此,您的代码将在运行后给出正确的输出:

      links <- links[,c(4,5,3)]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-21
        • 2013-01-21
        • 2022-11-11
        • 2018-04-14
        • 2015-11-20
        • 2022-01-16
        相关资源
        最近更新 更多