2017-03-23 86 views
0

比方说,我有两个图:net1net2具有相同的节点名称。我想联合net1net2成一个图形net然后从节点A添加一个新的边缘到节点A其中从部件net1第一节点A和从组件net2第二节点A。我曾尝试:如何在不使用data.frames的情况下合并两个igraphs对象?

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") 

enter image description here

我正在寻找像function_union(net1, net2)一个打造专业化的函数。

问题。是否可以将两个igraph对象不变换成data.frame对象并返回到igraphs对象?

回答

2

当您在同一个顶点上合并时,这两个相同的图形会合并为一个图形。建议使用不同的顶点创建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") 
相关问题