如何在不使用 data.frames 的情况下合并两个 igraphs 对象?
How to union two igraphs objects without using data.frames?
假设我有两个图:net1
和 net2
具有相同的节点名称。我想将 net1
和 net2
合并到一个图 net
中,然后从节点 A
到节点 A
添加一条新边,其中第一个节点 A
来自组件 net1
,第二个节点 A
来自组件 net2
。我试过:
library(igraph)
net1 <- graph_from_literal(A-B-C)
net2 <- graph_from_literal(A-B-C)
par(mfrow=c(2,2))
plot(net1, main="net1")
plot(net2, main="net2")
head <- "A"
tail <- "A"
AddEdge <- c( which(V(net1)$name == head),
which(V(net2)$name == tail))
net <- union(net1, net2)
#net <- graph.union(net1, net2, byname=F)
#net <- graph.union(net1, net2, byname=T)
# add edge
net <- add_edges(net, AddEdge, color = "red")
plot(net, main="union net1 and net2")
我正在寻找类似 function_union(net1, net2)
的内置函数。
问题。是否可以合并两个 igraphs 对象而不转换为 data.frame
对象并返回 igraphs
对象?
当您在相同的顶点上并集时,2 个相同的图会合并为 1 个图。建议创建 2 个具有不同顶点但具有相同标签的不同图,并集然后绘制。
library(igraph)
net1 <- graph_from_literal(A1-B1-C1)
net2 <- graph_from_literal(A2-B2-C2)
#union the 2 graphs and update the color of the edges
net <- union(net1, net2)
E(net)$color <- "gray"
#link the 2 graphs
net <- add_edges(net, which(V(net)$name %in% c("A1", "A2")), color="red")
#update the labels of the union graph
V(net)$label <- substr(V(net)$name, 1, 1)
#plot the union graph
plot(net, main="union net1 and net2")
假设我有两个图:net1
和 net2
具有相同的节点名称。我想将 net1
和 net2
合并到一个图 net
中,然后从节点 A
到节点 A
添加一条新边,其中第一个节点 A
来自组件 net1
,第二个节点 A
来自组件 net2
。我试过:
library(igraph)
net1 <- graph_from_literal(A-B-C)
net2 <- graph_from_literal(A-B-C)
par(mfrow=c(2,2))
plot(net1, main="net1")
plot(net2, main="net2")
head <- "A"
tail <- "A"
AddEdge <- c( which(V(net1)$name == head),
which(V(net2)$name == tail))
net <- union(net1, net2)
#net <- graph.union(net1, net2, byname=F)
#net <- graph.union(net1, net2, byname=T)
# add edge
net <- add_edges(net, AddEdge, color = "red")
plot(net, main="union net1 and net2")
我正在寻找类似 function_union(net1, net2)
的内置函数。
问题。是否可以合并两个 igraphs 对象而不转换为 data.frame
对象并返回 igraphs
对象?
当您在相同的顶点上并集时,2 个相同的图会合并为 1 个图。建议创建 2 个具有不同顶点但具有相同标签的不同图,并集然后绘制。
library(igraph)
net1 <- graph_from_literal(A1-B1-C1)
net2 <- graph_from_literal(A2-B2-C2)
#union the 2 graphs and update the color of the edges
net <- union(net1, net2)
E(net)$color <- "gray"
#link the 2 graphs
net <- add_edges(net, which(V(net)$name %in% c("A1", "A2")), color="red")
#update the labels of the union graph
V(net)$label <- substr(V(net)$name, 1, 1)
#plot the union graph
plot(net, main="union net1 and net2")