Skip to content

Instantly share code, notes, and snippets.

@nicolewhite
Created June 9, 2014 11:57
Show Gist options
  • Save nicolewhite/376f0ee878a740092f69 to your computer and use it in GitHub Desktop.
Save nicolewhite/376f0ee878a740092f69 to your computer and use it in GitHub Desktop.
Community detection in R.
library(RNeo4j)
library(igraph)
graph = startGraph("http://localhost:7474/db/data/")
clear(graph)
create_users = "FOREACH (i IN RANGE(1,50) | CREATE (:User {id:i, name:'name ' + i}))
WITH 1 AS dummy
MATCH (a:User), (b:User)
WITH a, b
WHERE (a.id % 5 = b.id % 5 AND rand() < 0.2) OR rand() < 0.005
CREATE UNIQUE (a)-[:KNOWS]-(b)"
cypher(graph, create_users)
addConstraint(graph, "User", "name")
adj = adjacencyMatrix(graph,
label = "User",
key = "name",
type = "KNOWS")
g = graph.adjacency(adj, diag = F)
g$layout = layout.fruchterman.reingold(g)
plot(g)
V(g)$color = edge.betweenness.community(g, directed = F)$membership
plot(g)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment