Plot在同一Graph Igraph上收集子图的社区

时间:2017-05-31 14:04:53

标签: r plot igraph

我在R中有一个非定向网络,当在igraph中绘制时看起来像:

plot(g2)

enter image description here

从这个主图中,我提取所有具有最小顶点= 4的簇/子图:

modules <- decompose.graph(g2, min.vertices = 4)
out <- modules[order(sapply(modules, ecount), decreasing=T)]

,并提供:

> head(modules)
[[1]]
IGRAPH UN-- 4 4 -- 
+ attr: name (v/c), value (e/n), sourceID (e/n), targetID (e/n)
+ edges (vertex names):
[1] 333--342 333--324 333--323 342--324

[[2]]
IGRAPH UN-- 4 4 -- 
+ attr: name (v/c), value (e/n), sourceID (e/n), targetID (e/n)
+ edges (vertex names):
[1] 27--25 27--20 25--20 25--21

[[3]]
IGRAPH UN-- 4 3 -- 
+ attr: name (v/c), value (e/n), sourceID (e/n), targetID (e/n)
+ edges (vertex names):
[1] 662--657 662--705 706--657

[[4]]
IGRAPH UN-- 4 3 -- 
+ attr: name (v/c), value (e/n), sourceID (e/n), targetID (e/n)
+ edges (vertex names):
[1] 793--792 793--795 793--794

[[5]]
IGRAPH UN-- 4 3 -- 
+ attr: name (v/c), value (e/n), sourceID (e/n), targetID (e/n)
+ edges (vertex names):
[1] 746--747 746--722 746--721

[[6]]
IGRAPH UN-- 4 3 -- 
+ attr: name (v/c), value (e/n), sourceID (e/n), targetID (e/n)
+ edges (vertex names):
[1] 285--284 285--286 287--284

我可以通过执行以下操作将这些图表绘制成一个图表:

vertexes <- character()
data_frames <- list()
for(i in 1:length(out)) {
  vertexes[i] <- list(vertex.attributes(out[[i]])$name)
  data_frames[[i]] <- get.data.frame(out[[i]])
}
sub_nodes = unlist(vertexes)
subv <- sub_nodes
g3 <- induced.subgraph(graph=g2,vids=subv)
plot(g3)

enter image description here

我可以计算这个主图的中间性并显示结果:

wc <- edge.betweenness.community(g3, weights = NULL
                                 ,directed = FALSE,bridges = TRUE)
plot(wc, g3)

enter image description here

这会将每个子群集拆分为一个社区。

我想要的是为每个子群集评估的 介于性

我通过以下方式计算每个子图的介于:

betweenness_sub <- list()
for(i in 1:length(out)) {
  btws <- edge.betweenness.community(out[[i]], weights = NULL
                                                   ,directed = FALSE,bridges = TRUE)
  print(btws)
  betweenness_sub[[i]] <- btws
}

这给出了:

> head(betweenness_sub)
[[1]]
IGRAPH clustering edge betweenness, groups: 2, mod: 0.17
+ groups:
  $`1`
  [1] "646" "647" "723"

  $`2`
  [1] "713" "714" "710"


[[2]]
IGRAPH clustering edge betweenness, groups: 1, mod: 0
+ groups:
  $`1`
  [1] "685" "684" "686" "691" "690"


[[3]]
IGRAPH clustering edge betweenness, groups: 1, mod: 0
+ groups:
  $`1`
  [1] "719" "718" "734" "753" "715"


[[4]]
IGRAPH clustering edge betweenness, groups: 1, mod: 0
+ groups:
  $`1`
  [1] "702" "757" "720" "735"


[[5]]
IGRAPH clustering edge betweenness, groups: 1, mod: 0
+ groups:
  $`1`
  [1] "83" "80" "88" "87" "81"


[[6]]
IGRAPH clustering edge betweenness, groups: 1, mod: 0
+ groups:
  $`1`
  [1] "333" "342" "324" "323"

我可以通过以下方式单独绘制这些:

plot(betweenness_sub[[1]], out[[1]])

Beautiful Butterfly

我现在要做的是绘制每个子群集的社区,但完全像第三个绘图中那样。

这次突出显示的社区将是上面列出的那些..

有没有办法重新组合&#34; &#34;结合&#34;或将这些单独的子图社区网格化为完整的图形?

作为另一个例子,如果我们取最小顶点= 7,并运行上面的代码,我们得到:

vertexes <- character()
data_frames <- list()
for(i in 1:length(out)) {
  vertexes[i] <- list(vertex.attributes(out[[i]])$name)
  data_frames[[i]] <- get.data.frame(out[[i]])
}
sub_nodes = unlist(vertexes)
subv <- sub_nodes
g3 <- induced.subgraph(graph=gg,vids=subv)
plot(g3)

wc <- edge.betweenness.community(g3, weights = E(out)$value
                                 ,directed = FALSE,bridges = TRUE)
# g3 <- delete.edges(g3, wc$removed.edges[seq(length=1)])
plot(wc, g3)

What I have

然而,如果我们单独考虑最大的集群,我们会得到:

plot(betweenness_sub[[1]], out[[1]])

enter image description here

我希望在第二张图像中按照与第一张图像相同的排列在各个群集上计算社区。有什么想法吗?

编辑3.用于创建上图(最小顶点= 7)的数据如下:

> get.data.frame(g3)
   from   to  value sourceID targetID
1    74   80 0.2829   255609   262854
2    74   61 0.2880   255609   179585
3    80 1085 0.2997   262854  3055482
4   476  502 0.2714  1442372  1475074
5   476  524 0.2963  1442372  1532560
6   476  479 0.2214  1442372  1448436
7   476  497 0.2714  1442372  1466635
8   476  498 0.2714  1442372  1467171
9   476  500 0.2714  1442372  1468099
10  502  479 0.2714  1475074  1448436
11  502  497 0.1215  1475074  1466635
12  502  498 0.1215  1475074  1467171
13  502  500 0.1215  1475074  1468099
14  524  479 0.2963  1532560  1448436
15 1045 1046 0.1842  2970629  2971615
16 1046 1085 0.2963  2971615  3055482
17 1046 1154 0.2714  2971615  3087803
18 1047 1120 0.2886  2972434  3076314
19 1085 1154 0.2577  3055482  3087803
20 1085 1187 0.2850  3055482  3101131
21 1085 1209 0.2850  3055482  3110186
22 1097 1159 0.2922  3062163  3090708
23 1097 1087 0.2256  3062163  3058341
24 1097 1158 0.2922  3062163  3090707
25 1097 1160 0.2922  3062163  3090709
26 1097 1177 0.2577  3062163  3099199
27 1098 1159 0.2922  3062164  3090708
28 1098 1087 0.2256  3062164  3058341
29 1098 1158 0.2922  3062164  3090707
30 1098 1160 0.2922  3062164  3090709
31 1098 1177 0.2577  3062164  3099199
32 1118 1119 0.1587  3076246  3076248
33 1118 1090 0.2256  3076246  3058962
34 1119 1120 0.1736  3076248  3076314
35 1119 1144 0.2035  3076248  3085240
36 1119 1145 0.2035  3076248  3085241
37 1154 1243 0.2577  3087803  3130848
38 1154 1187 0.2305  3087803  3101131
39 1154 1209 0.2305  3087803  3110186
40 1154 1244 0.2577  3087803  3131379
41 1159 1087 0.2922  3090708  3058341
42 1243 1187 0.1488  3130848  3101131
43 1243 1209 0.1488  3130848  3110186
44 1243 1244 0.1215  3130848  3131379
45 1243 1281 0.2997  3130848  3255811
> g <- graph.data.frame(get.data.frame(g3), directed=FALSE)
> gg
IGRAPH UN-- 33 45 -- 
+ attr: name (v/c), value (e/n), sourceID (e/n), targetID (e/n)
+ edges (vertex names):
 [1] 74  --80   74  --61   80  --1085 476 --502  476 --524  476 --479  476 --497  476 --498  476 --500  502 --479  502 --497  502 --498 
[13] 502 --500  524 --479  1045--1046 1046--1085 1046--1154 1047--1120 1085--1154 1085--1187 1085--1209 1097--1159 1097--1087 1097--1158
[25] 1097--1160 1097--1177 1098--1159 1098--1087 1098--1158 1098--1160 1098--1177 1118--1119 1118--1090 1119--1120 1119--1144 1119--1145
[37] 1154--1243 1154--1187 1154--1209 1154--1244 1159--1087 1243--1187 1243--1209 1243--1244 1243--1281

编辑2:

可以使用plotly包绘制多个群集:

par(mfrow=c(2,3))
par(mar = rep(2, 2))
for(i in 1:length(out)) {
  plot(betweenness_sub[[i]], out[[i]])
}

enter image description here

但我想知道如何使用igraph专门做这个,以保持圆形结构,如图所示。

1 个答案:

答案 0 :(得分:1)

下面是我的算法,它不是很优雅,但它似乎工作。基本的想法是从betweenness_sub获取组信息并将它们连接成一个大列表,例如subs,然后使用plot(g3, mark.groups = subs)使用来自的组信息绘制整个图表。 subs,即betweenness-sub。请注意,在我的代码中,graph是您发布的图data.frame

g2 <- graph.data.frame(graph, directed=FALSE)

modules <- decompose.graph(g2, min.vertices = 7)
out <- modules[order(sapply(modules, ecount), decreasing=T)]

vertexes <- character()
data_frames <- list()
for(i in 1:length(out)) {
    vertexes[i] <- list(vertex.attributes(out[[i]])$name)
    data_frames[[i]] <- get.data.frame(out[[i]])
}
sub_nodes = unlist(vertexes)
subv <- sub_nodes
g3 <- induced.subgraph(graph=g2,vids=subv)
plot(g3)

wc <- edge.betweenness.community(g3, weights = NULL
                                 ,directed = FALSE,bridges = TRUE)
plot(wc, g3)

betweenness_sub <- list()
for(i in 1:length(out)) {
    btws <- edge.betweenness.community(out[[i]], weights = NULL
                                       ,directed = FALSE,bridges = TRUE)
    print(btws)
    betweenness_sub[[i]] <- btws
}

subs <- list()
k <- 1
for (sub in betweenness_sub) {
    for (i in 1:length(sub)) {
        subs[[k]] <- sub[[i]]
        k = k + 1
    }
}

plot(g3, mark.groups = subs)