Search code examples
rgraphigraphmulti-layer

Multigraph from two simple graphs in igraph?


How to get a multigraph from two or more simple graphs in R igraph?

G <- graph_from_literal(1-2:3-4-5:6)
E(G)$at <- rep.int("a",6)
G2 <- graph_from_literal(6-7-5-4)
E(G2)$at <- c("b","b","b")
G3 <- graph.union(G, G2)
E(G3)$at_1
E(G3)$at_2
is.simple(G3)
plot(G3)

Note that both graphs have an edge between vertices 4 and 5, I would expect a multigraph as a result, rather than a simple graph. There is another option instead of graph.union? Thanks


Solution

  • Herewith an elaboration of Szabolcs's suggestion.

    DF3 <- rbind(as_data_frame(G), as_data_frame(G2))
    G3b  <- graph_from_data_frame(DF3, directed=FALSE)
    dev.new()
    plot(G3b, edge.color=ifelse(E(G3b)$at == "a", "black", "red"), edge.label=E(G3b)$at)