假设我有两个图: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
对象?
发布于 2017-03-23 09:32:15
当你在相同的顶点上合并时,这两个图,是相同的,折叠成一个图。建议创建两个不同的图,具有不同的顶点,但有相同的标签,并然后绘制。
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")
https://stackoverflow.com/questions/42970563
复制相似问题