2

How can I visualize communities if there are overlapping communities in the graph? I can use any module in python (networkx, igraph, matplotlib, etc.) or R.

For example, information on nodes, edges, and the nodes in each community is given as follows. Note that node G spans two communities.

list_nodes = ['A', 'B', 'C', 'D','E','F','G','H','I','J']
tuple_edges = [('A','B'),('A','C'),('A','D'),('B','C'),('B','D'), ('C','D'),('C','E'),
              ('E','F'),('E','G'),('F','G'),('G','H'),
              ('G','I'), ('G','J'),('H','I'),('H','J'),('I','J'),]
list_communities = [['A', 'B', 'C', 'D'],['E','F','G'],['G', 'H','I','J']]

I would like a plot that visualizes the community as shown below.

enter image description here

In networkx, it is possible to colour-code each node like this post, but this method is not suitable when communities overlap.

In igraph, communities can be visualised using the community extraction method included in the package, as described in this post. However, in my case I want to define communities using the list of nodes contained in each community.

mimi
  • 61
  • 4
  • Question is similar to `Plot communities with igraph`, https://stackoverflow.com/questions/26913419/plot-communities-with-igraph – clp Nov 29 '22 at 19:28

1 Answers1

3

Below is an option for igraph within R.


I think you may have to annotate the community info manually (see grp below) and then use it when plotting, e.g.,

g <- graph_from_data_frame(df, directed = FALSE)
grp <- lapply(
  groups(cluster_edge_betweenness(g)),
  function(x) {
    c(
      x,
      names(which(colSums(distances(g, x) == 1) > 1))
    )
  }
)
plot(g, mark.groups = grp)

or using max_cliques with minimal size 3 (thank @clp's comment)

g <- graph_from_data_frame(df, directed = FALSE)
grp <- max_cliques(g, min = 3)
plot(g, mark.groups = grp)

enter image description here

Data

df <- data.frame(
  from = c("A", "A", "A", "B", "B", "C", "C", "E", "E", "F", "G", "G", "G", "H", "H", "I"),
  to = c("B", "C", "D", "C", "D", "D", "E", "F", "G", "G", "H", "I", "J", "I", "J", "J")
)
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81