顶点标签从 id 更改为 igraph 中的另一列

问题描述

我正在创建一个以 id 作为顶点名称的图形。但是我想将顶点标签更改为另一个列值。我该怎么做?

df1 中的列是 ID、名称,我希望顶点标签名称

我的代码

df1:
ID  NAME
1   Ada
2   Cora
3   Louise
            
df2:
SOURCE  TARGET  TYPE       ID  WEIGHT
1       2       DIRECTED   2   2   
1       3       DIRECTED   1   2
2       1       DIRECTED   3   1



  g = graph.data.frame(d = df2,directed = TRUE,vertices = df1);    
    V(g)$size<-degree(g)
        plot(g,layout=layout.circle,main="circle",vertex.label.dist=0.4,vertex.label=V(g)$id,vertex.label.cex=1,edge.arrow.size=0.

谢谢

解决方法

你可以试试

g %>%
    set_vertex_attr(
        name = "name",value = V(.)$NAME
    )

给出

IGRAPH 19e91db DN-- 3 3 --
+ attr: name (v/c),NAME (v/c),TYPE (e/c),ID (e/n),WEIGHT (e/n)
+ edges from 19e91db (vertex names):
[1] Ada ->Cora   Ada ->Louise Cora->Ada