Home > Mobile >  igraph in R: Missing edges when merging 2 graphs
igraph in R: Missing edges when merging 2 graphs

Time:01-22

I have made 2 distinct network in igraph and want to merge them into one graph. Each edge of each individual graph needs to be present in the combined graph, but there are multiple edges that are missing. How can I get each edge to appear in the merged graph?

A reproducible example

edge_1 <- data.frame(from = c("E", "D", "D", "A", "E", "D", "D", "B", "F", "A", "F"), to = c("B", "C", "F", "E", "A", "A", "B", "C", "A", "E", "D"))
vtx_1 <- data.frame(Names = c("A", "B", "C", "D", "E", "F"))

edge_2 <- data.frame(from = c("C", "E", "F", "A", "E", "A", "B", "A", "D", "B", "D"), to = c("F", "C", "C", "E", "A", "C", "C", "E", "F", "C", "C"))
vtx_2 <- data.frame(Names = c("A", "B", "C", "D", "E", "F"))

library(igraph)
g1 <- graph_from_data_frame(edge_1, vertices = vtx_1, directed = TRUE)
g2 <- graph_from_data_frame(edge_2, vertices = vtx_2, directed = TRUE)

plot(g1 %du% g2, 
     edge.arrow.size = 0.5)

plot(g1 %u% g2, 
     edge.arrow.size = 0.5)

The results of these graphs are the following: Disjointed and Union Graphs

The Issue

The Disjointed graph: Top graph: Vertex C has 6 incoming edges; 1 outgoing edge Bottom graph: Vertex C has 2 incoming edges

The Union graph: Vertex C has 6 incoming edges; 1 outgoing edge

The union graph needs to show 8 incoming edges (the total of all incoming edges for both graphs) and 1 outgoing graph.

To note: other vertices (not just vertex C) also display missing edges in the union graph.

Any ideas on how all total edges can be shown in the union graph?

Thank-you!

CodePudding user response:

One way to get what you want is to just use the edgelists again.

edge_merged = rbind(edge_1, edge_2)
vtx_merged = data.frame(Names = union(vtx_1$Names, vtx_2$Names))

gmerged <- graph_from_data_frame(edge_merged, vertices = vtx_merged, 
    directed = TRUE)
plot(gmerged, edge.arrow.size = 0.5)

Merged graphs

If you don't have edgelists, you can generate them using as_edgelist.

  • Related