title: Louvain community detection in R using igraph - format of edges and vertices
body: <p>I have a correlation matrix of scores that I would like to run community detection on using the Louvain method in igraph, in R. I converted the correlation matrix to a distance matrix using <code>cor2dist</code>, as below:</p>
  
  <p><code>distancematrix &lt;- cor2dist(correlationmatrix)</code></p>
  
  <p>This gives a 400 x 400 matrix of distances from 0-2. I then made the list of edges (the distances) and vertices (each of the 400 individuals) using the below method from <a href="http://kateto.net/networks-r-igraph" rel="nofollow noreferrer">http://kateto.net/networks-r-igraph</a> (section 3.1). </p>
  
  <pre><code>library(igraph)
  test &lt;- as.matrix(distancematrix)
  mode(test) &lt;- "numeric"
  test2 &lt;- graph.adjacency(test, mode = "undirected", weighted = TRUE, diag = TRUE)
  E(test2)$weight
  get.edgelist(test2)
  </code></pre>
  
  <p>From this I then wrote csv files of the 'from' and 'to' edge list, and corresponding weights:</p>
  
  <pre><code>edgeweights &lt;-E(test2)$weight
  write.csv(edgeweights, file = "edgeweights.csv")
  fromtolist &lt;- get.edgelist(test2)
  write.csv(fromtolist, file = "fromtolist.csv")
  </code></pre>
  
  <p>From these two files I produced a .csv file called "nodes.csv" which simply had all the vertex IDs for the 400 individuals:</p>
  
  <pre><code>id
  1
  2
  3
  4
  ...
  400
  </code></pre>
  
  <p>And a .csv file called "edges.csv", which detailed 'from' and 'to' between each node, and provided the weight (i.e. the distance measure) for each of these edges:</p>
  
  <pre><code>from    to   weight
  1       2    0.99
  1       3    1.20
  1       4    1.48
  ...
  399     400  0.70
  </code></pre>
  
  <p>I then tried to use this node and edge list to create an igraph object, and run louvain clustering in the following way:</p>
  
  <pre><code>nodes &lt;- read.csv("nodes.csv", header = TRUE, as.is = TRUE)
  edges &lt;- read.csv("edges.csv", header = TRUE, as.is = TRUE)
  clustergraph &lt;- graph_from_data_frame(edges, directed = FALSE, vertices = nodes)
  clusterlouvain &lt;- cluster_louvain(clustergraph)
  </code></pre>
  
  <p>Unfortunately this did not do the louvain community detection correctly. I expected this to return around 2-4 different communities, which could be plotted similarly to <a href="https://stackoverflow.com/questions/24513339/how-to-find-measures-after-community-detection-in-igraph-r?rq=1">here</a>, but <code>sizes(clusterlouvain)</code> returned:</p>
  
  <pre><code>Community sizes
   1 
   400
  </code></pre>
  
  <p>indicating that all individuals were sorted into the same community. The clustering also ran immediately (i.e. with almost no computation time), which also makes me think it was not working correctly. </p>
  
  <p><strong>My question is:</strong> Can anyone suggest why the cluster_louvain method did not work and identified just one community? I think I must be specifying the distance matrix or edges/nodes incorrectly, or in some other way not giving the correct input to the cluster_louvain method. I am relatively new to R so would be very grateful for any advice. I have successfully used other methods of community detection on the same distance matrix (i.e. k-means) which identified 2-3 communities, but would like to understand what I have done wrong here.</p>
  
  <p>I'm aware there are multiple other queries about using igraph in R, but I have not found one which explicitly specifies the input format of the edges and nodes (from a correlation matrix) to get the louvain community detection working correctly.</p>
  
  <p>Thank you for any advice! I can provide further information if helpful.</p>