首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在不使用data.frames的情况下合并两个igraphs对象?

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

Stack Overflow用户
提问于 2017-03-23 08:19:08
回答 1查看 162关注 0票数 0

假设我有两个图:net1net2,它们的节点名称相同。我想将net1net2合并成一个图net,然后从节点A添加一个新的边缘到节点A,其中第一个节点A来自组件net1,第二个节点A来自组件net2。我试过:

代码语言:javascript
运行
复制
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对象?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-23 09:32:15

当你在相同的顶点上合并时,这两个图,是相同的,折叠成一个图。建议创建两个不同的图,具有不同的顶点,但有相同的标签,并然后绘制。

代码语言:javascript
运行
复制
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")
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42970563

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档