如何在不使用data.frames的情况下结合两个igraphs对象?

时间:2017-03-23 08:19:09

标签: r igraph

假设我有两个图表:topic_branchnet1具有相同的节点名称。我想将net2net1合并到一个图表net2中,然后将新边缘从节点net添加到节点A,其中第一个节点{来自组件A的{​​1}}和来自组件A的第二个节点net1。我试过了:

A

enter image description here

我正在寻找像net2这样的建筑功能。

问题。是否可以将两个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") 对象并返回function_union(net1, net2)个对象?

1 个答案:

答案 0 :(得分:2)

当你在相同的顶点上结合时,相同的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")