Chuck
Chuck

Reputation: 3852

igraph split graph into clusters

I have some undirected graph:

enter image description here

with data:

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

I am using two methods for community detection.

Using edge.betweenness.community I can split communities by calculating edge betweenness:

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

enter image description here

Using cluster_optimal I can split communities by maximising modularity:

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

Data for 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

At this point, I want to split these graphs into individual communities. This is done by cutting those edges that are highlighted red.

I can do this for edge.betweenness.community by returning those edges which are to be cut by doing:

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

I can then cut those edges:

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

enter image description here

But if I try this for cluster_optimal:

co$removed.edges
NULL    

It looks like cluster_optimal has no attribute removed$edges

I then looked in the documentation (http://igraph.org/r/doc/communities.html) and tried cut_at and cutat:

> 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

This has not worked either, even though co should be a community object.

How can I split my cluster_optimal graph into the individual communities like I did with edge.betweenness.community?


Trying the suggestion posted, when i try to execute

crossing(co, out)

I get the error:

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 

Upvotes: 6

Views: 4033

Answers (1)

emilliman5
emilliman5

Reputation: 5956

It looks like for all communities you can use the crossing function to get which edges cross communities (see the docs for the communities 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")

enter image description here

Session Info

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 

Upvotes: 6

Related Questions