Hello,
I am trying to write network data to GraphML, using igraphs's write_graph()
function. However, after successfully creating the graph object, setting the vertex attributes, and then writing the graph as GraphML, when I open the graph file in Gephi, I discover that the node attributes do not match the right nodes. Below is my R script of a reprex:
# nodes data frame
ndf <- data. Frame(
id = 1:5,
names = c("Mandy","Hogan","Chioma","Tayo","Ali"),
age = c(21,25,20,26,30),
color = c("blue","green","red","orange","purple")
)
# edges data frame
edf <- data. Frame(
Vertex1 = c("Hogan","Hogan","Tayo","Ali","Chioma","Mandy","Chioma","Mandy"),
Vertex2 = c("Mandy","Chioma","Mandy","Hogan","Tayo","Ali","Tayo","Ali")
)
# for loop to combine the edges into one vector
edf_df <- c()
for(i in 1:nrow(edf)){
result <- c(edf[i,]$Vertex1,edf[i,]$Vertex2)
edf_df <- append(edf_df,result)
}
# creating a new data frame of the combined edges
edf_cmb <- data.frame(
edges = edf_df
)
# extracting the attributes for each node from the nodes data frame
age <- c()
for(i in 1:nrow(edf_cmb)){
age <- append(age,filter(ndf,ndf$names == edf_cmb$edges[[i]][1])[[3]][1])
}
color <- c()
for(i in 1:nrow(edf_cmb)){
color <- append(color,filter(ndf,ndf$names == edf_cmb$edges[[i]][1])[[4]][1])
}
# adding the attributes to the data frame of combined edges
edf_cmb$age <- age
edf_cmb$color <- color
print(edf_cmb)
edges age color
1 Hogan 25 green
2 Mandy 21 blue
3 Hogan 25 green
4 Chioma 20 red
5 Tayo 26 orange
6 Mandy 21 blue
7 Ali 30 purple
8 Hogan 25 green
9 Chioma 20 red
10 Tayo 26 orange
11 Mandy 21 blue
12 Ali 30 purple
13 Chioma 20 red
14 Tayo 26 orange
15 Mandy 21 blue
16 Ali 30 purple
# creating the graph object
edf_gph <- make_graph(edf_df,directed = T)
# adding the vertex attributes to the graph object
{
edf_gph <- set_vertex_attr(edf_gph,"age",value = edf_cmb$age)
edf_gph <- set_vertex_attr(edf_gph,"color",value = edf_cmb$color)
}
# writing the graph file as a GraphML
write_graph(edf_gph,file = "edf.graphml",format = "graphml")
However, when I import the GraphML file into a program like Gephi or NodeXL, the node attributes do not match the right node anymore like is displayed in the above table (I apologise that the table looks awful), and I do not know why this is the case.
Please, I would greatly appreciate any helpful suggestions in this regard. Thanks!