Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertex/node attributes for igraph objects

Tags:

r

igraph

I am working with network analysis data in igraph Here is an example dataframe

df_edgelist=structure(list(Nominator = structure(c(6L, 4L, 7L, 8L, 1L, 2L, 
3L, 5L), .Label = c("Andrea", "Dan", "Dan", "Greg", "Jerry", 
"Jim", "Rachel", "Sarah"), class = "factor"), Nominee = structure(c(5L, 
2L, 8L, 1L, 7L, 3L, 6L, 4L), .Label = c("Andrea", "Dan", "Greg", 
"Jeff", "Jerry", "Jerry", "Sarah", "Tim"), class = "factor"), 
    Age_Nominator = c(24L, 25L, 29L, 45L, 43L, 67L, 67L, 45L)), class = "data.frame", row.names = c(NA, 
-8L))

From the documentation here I get the impression that the graph_from_dataframe function will treat that 3rd column Age_Nominator as a vertex attribute if I also supply it with another dataframe and set the name of that dataframe in the vertices argument of graph_from_dataframe. Is that correct or is that 3rd column in df_edgelist still an edge attribute?

like image 751
Jin Avatar asked Sep 13 '26 08:09

Jin


1 Answers

The two arguments of interest in graph_from_data_frame are d and vertices. As discussed in the comments, extra columns in d will be edge attributes, while extra columns in vertices (with the first columns being vertex names) will be vertex attributes.

Further,

If vertices is not NULL then the symbolic edge list given in d is checked to contain only vertex names listed in vertices.

means that the first two columns of d cannot mention any vertices that are not present in vertices. On the other hand, if vertices has some extra vertices, that will raise no issue and they will simply be isolated.

For instance,

df_vertices <- data.frame(someNames = c("NewName", as.character(unique(unlist(df_edgelist[, 1:2])))))
df_vertices$Age <- 20 + 1:nrow(df_vertices)
df_vertices
#    someNames Age
# 1    NewName  21
# 2        Jim  22
# 3       Greg  23
# 4     Rachel  24
# 5      Sarah  25
# 6     Andrea  26
# 7        Dan  27
# 8      Jerry  28
# 9        Tim  29
# 10      Jeff  30

in this way we consider all the necessary vertices and add an extra NewName. Then

g <- graph_from_data_frame(df_edgelist, vertices = df_vertices)
# V(g)$Age
#  [1] 21 22 23 24 25 26 27 28 29 30
V(g)$name
#  [1] "NewName" "Jim"     "Greg"    "Rachel"  "Sarah"   "Andrea"  "Dan"     "Jerry"   "Tim"    
# [10] "Jeff"   
E(g)
# + 8/8 edges from 7f024f1 (vertex names):
# [1] Jim   ->Jerry  Greg  ->Dan    Rachel->Tim    Sarah ->Andrea Andrea->Sarah  Dan   ->Greg  
# [7] Dan   ->Jerry  Jerry ->Jeff  

as expected. In case you want to avoid those isolated vertices, as vertices you may instead specify

df_vertices[df_vertices$someNames %in% as.character(unique(unlist(df_edgelist[, 1:2]))), ]
#    someNames Age
# 2        Jim  22
# 3       Greg  23
# 4     Rachel  24
# 5      Sarah  25
# 6     Andrea  26
# 7        Dan  27
# 8      Jerry  28
# 9        Tim  29
# 10      Jeff  30
like image 175
Julius Vainora Avatar answered Sep 15 '26 22:09

Julius Vainora



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!