r - igraph 将图分割成簇

标签 r graph igraph

我有一些无向图:

enter image description here

有数据:

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)

enter image description here

使用 cluster_optimal我可以通过最大化模块化来 split 社区:
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"

enter image description here

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

我可以为 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)

enter image description here

但如果我尝试 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应该是一个社区对象。

如何拆分我的 cluster_optimal就像我对 edge.betweenness.community 所做的那样,绘制到各个社区?

尝试发布的建议,当我尝试执行时
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获取哪些边缘跨社区的功能(请参阅社区对象的文档)

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")
enter image description here
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 

关于r - igraph 将图分割成簇,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44372499/

相关文章:

r - 如何使用 R 中的 igraph 根据长度选择图的某些路径

c++ - 有没有办法检查在 C++ 中复制或分配对象是否安全?

r - 将 r 中节点向量的顺序从级别顺序更改为中缀顺序

css - 如何修改 Shiny 默认 CSS 来改变 sidebarPanel 的背景颜色?

memory - 用小内存在大图上进行广度优先搜索

oracle - 无向图中的最小 ID

algorithm - 使用旧调试版本的符号来符号化剥离的二进制文件(不精确的图形匹配)

r - 在有向图中设置方向

r - docker 与 Shiny 的服务器专业版问题

r - 你能告诉我这个错误是什么意思吗?