igraph R 中的顶点标签

Vertex Labels in igraph R

我正在使用 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 table中表示为sourceIDtargetID

所以,对于:

  source target  value sourceID targetID
1      3      4 0.6245  1450552  1519842

这将显示为:

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

而不是:

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

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

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

我怎样才能做到这一点?

我咨询了以下问题无果:

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

您可以这样指定标签:

 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")

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

"If vertices is NULL, then the first two columns of d are used as a symbolic edge list and additional columns as edge attributes."

因此,您的代码将在 运行:

之后给出正确的输出
links <- links[,c(4,5,3)]

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

关键是在 plot() 中使用 vertex.label 属性和 select 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)

根据以上数据,得出: