CoderAna
CoderAna

Reputation: 1

Vertex label to change from id to another column in igraph

I am creating a graph that's taking the id as vertex names. However I want to change the vertex label to another column values. How can I do that?

Columns in df1 are ID, name and I want the vertex labels to be name

My code:

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.

Thanks

Upvotes: 0

Views: 274

Answers (1)

ThomasIsCoding
ThomasIsCoding

Reputation: 102529

You can try

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

which gives

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 

Upvotes: 0

Related Questions