2017-05-19 211 views
0

我使用igraph绘制非定向力网络。igraph中的顶点标签

我有nodes一个数据帧和links如下:

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

enter image description here

在这个情节,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 ary为igraph绘图)。

我知道我需要以某种方式matchmap代理索引他们的nodes数据帧中的相应name。然而,由于我不知道igraph绘制标签的顺序,所以我很茫然。

我该如何做到这一点?

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

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

回答

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) 

enter image description here

你也可以传

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

如果您需要其他节点属性,则可以使用vertices参数。

数据:

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

谢谢您的回答。真的很有用。当你说'你可以传递...到顶点参数'时,你究竟是什么意思?你的意思是设置'vertex.attr = merge ....'? – Chuck

+0

我的意思是'graph.data.frame(...,vertices = merge(...))'。 – lukeA

+0

非常好,非常感谢。 – Chuck

1

的最简单的解决办法是重新排序的links的列,因为根据文件:

“如果顶点为NULL,则d的前两列被用作符号边缘列表和附加列作为边缘属性。“

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

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

谢谢!将尝试一下。 – Chuck

1

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

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) 

通过上面的数据,这给:

enter image description here