igraph 将图拆分为簇

igraph split graph into clusters

我有一些无向图:

有数据:

get.data.frame(out)
   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  1045 1046 0.1842  2970629  2971615
5  1046 1085 0.2963  2971615  3055482
6  1046 1154 0.2714  2971615  3087803
7  1085 1154 0.2577  3055482  3087803
8  1085 1187 0.2850  3055482  3101131
9  1085 1209 0.2850  3055482  3110186
10 1154 1243 0.2577  3087803  3130848
11 1154 1187 0.2305  3087803  3101131
12 1154 1209 0.2305  3087803  3110186
13 1154 1244 0.2577  3087803  3131379
14 1243 1187 0.1488  3130848  3101131
15 1243 1209 0.1488  3130848  3110186
16 1243 1244 0.1215  3130848  3131379
17 1243 1281 0.2997  3130848  3255811

> out
IGRAPH UN-- 12 17 -- 
+ attr: name (v/c), color (v/n), value (e/n), sourceID (e/n), targetID (e/n)
+ edges (vertex names):
 [1] 74  --80   74  --61   80  --1085 1045--1046 1046--1085 1046--1154 1085--1154 1085--1187 1085--1209 1154--1243 1154--1187 1154--1209
[13] 1154--1244 1243--1187 1243--1209 1243--1244 1243--1281

我正在使用两种方法进行社区检测。

使用edge.betweenness.community我可以通过计算边缘介数来分割社区:

wc <- edge.betweenness.community(out, weights = E(out)$value, directed = FALSE, bridges=TRUE)
plot(wc, out)

使用 cluster_optimal 我可以通过最大化模块化来拆分社区:

co <- cluster_optimal(out, weights = E(out)$value)
plot(co, out)

co 的数据:

> co
IGRAPH clustering optimal, groups: 3, mod: 0.27
+ groups:
  $`1`
  [1] "74" "80" "61"

  $`2`
  [1] "1045" "1046" "1085" "1154" "1187" "1209"

  $`3`
  [1] "1243" "1244" "1281"

在这一点上,我想将这些图表拆分为各个社区。这是通过切割那些突出显示为红色的边缘来完成的。

我可以为 edge.betweenness.community 执行此操作,方法是返回要切割的边:

wc$removed.edges
[1]  3  6  5 17 14 11  8 15  7 12  1  2  4  9 10 13 16

然后我可以切割那些边缘:

g2<-delete.edges(out, wc$removed.edges[seq(length=which.max(mods)-1)])
plot(g2)

但是如果我为 cluster_optimal 尝试这个:

co$removed.edges
NULL    

好像cluster_optimal没有属性removed$edges

然后我查看了文档 (http://igraph.org/r/doc/communities.html) 并尝试了 cut_atcutat:

> cutat(co, 3)
Error in cutat(co, 3) : Not a hierarchical communitity structure
> cut_at(co, 3)
Error in cut_at(co, 3) : Not a hierarchical communitity structure

这也没有用,即使 co 应该是一个社区对象。

如何像 edge.betweenness.community 那样将我的 cluster_optimal 图表拆分为各个社区?


尝试执行时发布的建议

crossing(co, out)

我收到错误:

Error: Each element must be either an atomic vector or a data     frame
.Problems: co, out.

R version 3.4.0 (2017-04-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.5 LTS

Matrix products: default
BLAS: /usr/lib/libblas/libblas.so.3.0
LAPACK: /usr/lib/lapack/liblapack.so.3.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8   
 [6] LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] plotly_4.7.0        ggplot2_2.2.1       tidyr_0.6.2         dtplyr_0.0.2        data.table_1.10.4   DT_0.2             
 [7] reshape2_1.4.2      igraph_1.0.1        dplyr_0.5.0         htmlwidgets_0.8     networkD3_0.4       htmltools_0.3.6    
[13] formattable_0.2.0.1

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.10      compiler_3.4.0    plyr_1.8.4        tools_3.4.0       digest_0.6.12     jsonlite_1.4      evaluate_0.10    
 [8] tibble_1.3.0      gtable_0.2.0      viridisLite_0.2.0 lattice_0.20-35   Matrix_1.2-10     DBI_0.6-1         stringr_1.2.0    
[15] httr_1.2.1        knitr_1.15.1      rprojroot_1.2     grid_3.4.0        R6_2.2.0          rmarkdown_1.5     purrr_0.2.2      
[22] magrittr_1.5      backports_1.0.5   scales_0.4.1      assertthat_0.2.0  colorspace_1.3-2  stringi_1.1.5     lazyeval_0.2.0   
[29] munsell_0.4.3 

对于所有社区,您似乎都可以使用 crossing 函数来获取哪些边穿过社区(请参阅社区文档 object)

library(igraph)

out <- data.frame(To = sample(1:10, 20, replace = T), From =sample(c(1,3,5,7,9), 20, replace = T))

out <- graph_from_edgelist(as.matrix(out), directed = F)
co <- cluster_optimal(out, weights = rpois(20, 2))
coGrph <- delete_edges(out, E(out)[crossing(co, out)])

par(mfrow=c(1,2))
plot(co, out, main="Cluster Optimal Communities")
plot(coGrph, main="Communities split")

Session 信息

R version 3.3.0 (2016-05-03)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.12.5 (unknown)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] igraph_1.0.1

loaded via a namespace (and not attached):
[1] magrittr_1.5 tools_3.3.0