在igraph中组合/合并两个图

时间:2017-12-28 00:11:20

标签: r igraph

我使用igraph创建了以下两个图:

t1terms <- c("fire",
            "people",
            "residents",
            "victims", 
            "affected", 
            "please",
            "can",
            "london",
            "support",
            "survivors")

t1labels <- as.vector(t1terms)

g<-graph.full(n=10, directed = FALSE, loops = FALSE) %>%
  set_vertex_attr("label", value = t1labels)


t2terms <- c("fire",
             "victims",
             "says",
             "people",
             "cladding",
             "police",
             "may",
             "will",
             "dead",
             "theresa")

t2labels <- as.vector(t2terms)

g1<-graph.full(n=10, directed = FALSE, loops = FALSE) %>%
  set_vertex_attr("label", value = t2labels)

我无法弄清楚如何在不复制公共节点的情况下合并两个图形。我真的很感激一些帮助。我试过'graph.union',但它不起作用。

谢谢,

Chamil

2 个答案:

答案 0 :(得分:3)

使用null的内置约定,并将顶点igraph放入每个label

name

抓取每个图表的属性和边缘列表,并V(g)$name <- V(g)$label V(g1)$name <- V(g1)$label 将它们组合在一起,创建组合属性rbind()和组合边列表data.frame,同时确保您只保留{ {1}} data.frame

unique()

使用verticesattrs <- rbind(as_data_frame(g, "vertices"), as_data_frame(g1, "vertices")) %>% unique() el <- rbind(as_data_frame(g), as_data_frame(g1)) 制作新图表:

attrs

答案 1 :(得分:0)

您可以通过将每个图表转换为边缘列表,加入边缘列表并将其制作成图形来获得联合。

EL  = matrix(get.vertex.attribute(g, "label")[get.edgelist(g)], ncol=2)
EL1 = matrix(get.vertex.attribute(g1, "label")[get.edgelist(g1)], ncol=2)
ELU = rbind(EL, EL1)
ELU = ELU[!duplicated(ELU),]
GU = graph_from_edgelist(ELU, directed=FALSE)

## To check the result
par(mfrow=c(1,3))
plot(g)
plot(g1)
plot(GU)

Union of two graphs