Using igraph, I aim to use community detection approaches, as i would like to draw a network layout that makes visible the distinct communities and their connections.
This is my code so far:
library(igraph)
dat=read.csv(file.choose(),header=TRUE) # choose an edgelist in .csv file format
I have a data frame with the following parameters;
> > Var1 = node 1 Var2 = node 2 Value = edges (markov chain probabilities) > >
> head (dat, 100)
> Var1 Var2 value
> 1 4 4 0.833333333
> 2 10 4 0.000000000
> 3 11 4 0.000000000
> 4 12 4 0.000000000
> 5 13 4 0.000000000
> 6 21 4 0.000000000
> 7 23 4 0.000000000
> 8 31 4 0.000000000
> 9 41 4 0.000000000
> 10 42 4 0.000000000
> 11 43 4 0.000000000
> 12 44 4 0.000000000
> 13 45 4 0.000000000
> 14 46 4 0.000000000
> 15 47 4 0.000000000
> 16 48 4 0.000000000
> 17 52 4 0.000000000
> 18 53 4 0.000000000
> 19 61 4 0.000000000
> 20 62 4 0.000000000
> 21 63 4 0.000000000
> 22 71 4 0.000000000
> 23 81 4 0.000000000
> 24 82 4 0.000000000
> 25 83 4 0.000000000
> 26 91 4 0.000000000
> 27 92 4 0.000000000
> 28 93 4 0.000000000
> 29 100 4 0.000000000
> 30 111 4 0.000000000
> 31 4 10 0.000000000
> 32 10 10 0.000000000
> 33 11 10 0.000000000
> 34 12 10 0.010695187
> 35 13 10 0.000000000
> 36 21 10 0.000000000
> 37 23 10 0.000000000
> 38 31 10 0.000000000
> 39 41 10 0.010869565
> 40 42 10 0.000000000
> 41 43 10 0.000000000
> 42 44 10 0.000000000
> 43 45 10 0.000000000
> 44 46 10 0.000000000
> 45 47 10 0.000000000
> 46 48 10 0.000000000
> 47 52 10 0.000000000
> 48 53 10 0.000000000
> 49 61 10 0.000000000
> 50 62 10 0.074074074
> 51 63 10 0.000000000
> 52 71 10 0.000000000
> 53 81 10 0.000000000
> 54 82 10 0.000000000
> 55 83 10 0.000000000
> 56 91 10 0.000000000
> 57 92 10 0.000000000
> 58 93 10 0.000000000
> 59 100 10 0.010526316
> 60 111 10 0.018867925
> 61 4 11 0.166666667
> 62 10 11 0.000000000
> 63 11 11 0.973409307
> 64 12 11 0.010695187
> 65 13 11 0.126126126
> 66 21 11 0.000000000
> 67 23 11 0.000000000
> 68 31 11 0.000000000
> 69 41 11 0.000000000
> 70 42 11 0.008928571
> 71 43 11 0.000000000
> 72 44 11 0.038461538
> 73 45 11 0.000000000
> 74 46 11 0.000000000
> 75 47 11 0.000000000
> 76 48 11 0.000000000
> 77 52 11 0.000000000
> 78 53 11 0.000000000
> 79 61 11 0.000000000
> 80 62 11 0.000000000
> 81 63 11 0.333333333
> 82 71 11 0.000000000
> 83 81 11 0.000000000
> 84 82 11 0.000000000
> 85 83 11 0.000000000
> 86 91 11 0.071428571
> 87 92 11 0.006622517
> 88 93 11 0.000000000
> 89 100 11 0.005263158
> 90 111 11 0.018867925
> 91 4 12 0.000000000
> 92 10 12 0.000000000
> 93 11 12 0.003798670
> 94 12 12 0.673796791
> 95 13 12 0.099099099
> 96 21 12 0.000000000
> 97 23 12 0.000000000
> 98 31 12 0.029702970
> 99 41 12 0.141304348
> 100 42 12 0.017857143
>
> dim (dat)
> [1] 900 3
g = graph.data.frame(dat[,c('Var1','Var2')], directed = F) # coerces the data into a two-column matrix format that igraph likes
cluster=cluster_walktrap(g)
list=groups (cluster)
g$value<-cluster$membership[as.character(g$Var1)]
g <- simplify(g) # remove loops and multiple edges
plot( cluster,g, vertex.size = 5, edge.width = .1)
The output doesn't make any sense to me. Could you help me please? Thanks
Using a very minor change to your code, I get an understandable result (although I am only using the 100 points that you provided). Your code ran simplify
after you ran cluster_walktrap
. I ran simplify
first. I also set the random seed so that the results would be reproducible. I get.
library(igraph)
g = graph.data.frame(dat[,c('Var1','Var2')], directed = F) # coerces the data into a two-column matrix format that igraph likes
g <- simplify(g) # remove loops and multiple edges
cluster=cluster_walktrap(g)
list=groups (cluster)
g$value<-cluster$membership[as.character(g$Var1)]
set.seed(123)
plot(cluster,g, vertex.size = 5, edge.width = .1)
This pretty clearly has two groups; blue nodes in one group, orange nodes in the other. Links that connect nodes in the same group are black. Those links that connect nodes in different groups are in red.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.