Reputation: 1116
I have read the can-an-igraph-object-have-directed-and-undirected-edges.
I have two graphs gUed
and gDed
.
The gUed
is the undirected graph and the gDed
is the directed one.
I need to union these graph into one.
I made this union with the edge list.
library(igraph)
gDed <- graph.formula(1-+2, 2-+3, 3-+4, 4-+5)
gUed <- graph.formula(6--1, 7--2, 8--4, 9--5)
EL = as_edgelist(gDed, names = TRUE)
EL1 = as_edgelist(gUed, names = TRUE)
ELU = rbind(EL, EL1)
ELU = ELU[!duplicated(ELU),]
GU = graph_from_edgelist(ELU)
## To check the result
par(mfrow=c(1,3))
plot(gDed)
plot(gUed)
plot(GU)
But I have a problem: in new graph GU
all edges are directed.
Question. How to union two graphs and save directed and undirected components in new graph?
Upvotes: 0
Views: 131
Reputation: 2435
A network has to be either directed or undirected. That said, you can work around that, a bit.
Similar to the answer you found, you create a directed network, and pass in an argument that keeps track of whether the edge is directed or not. You can use this when you plot, or to subset your network:
E(gDed)$type <- "dir"
E(gUed)$type <- "undir"
g <- graph_from_data_frame(
rbind(
as_data_frame(gDed, what = "edges"),
as_data_frame(gUed, what = "edges")
)
)
plot(g,
edge.arrow.mode = ifelse(E(g)$type=="undir", "-", ">"))
g_sub1 <- subgraph.edges(g, eids = E(g)[E(g)$type=="undir"] )
plot(g_sub1,
edge.arrow.mode = ifelse(E(g_sub1)$type=="undir", "-", ">"))
g_sub2 <- subgraph.edges(g, eids = E(g)[E(g)$type!="undir"] )
plot(g_sub2,
edge.arrow.mode = ifelse(E(g_sub2)$type=="undir", "-", ">"))
If there are double edges, one directed and one undirected, they will both be in g, leading to weird plots. You could fix this by creating a third type of edge, which will be either directed or undirected, depending on what you want to do.
Upvotes: 1