# R and iGraph: Colouring Community Nodes by attributes

When plotting the results of community detection on networks, sometimes one is interested in more than the connections between nodes. These network relations are usually multidimensional and you might want to represent other aspects other than the network links between nodes.

Plotting node attributes in R and iGraph might be tricky as the documentation is not always clears. In this example we’ll be using the Davis Dataset from the iGraph repository. This dataset was collected by Davis et al1 in the 1930s and represents the observed attendance at 14 social events by 18 Southern women.

Let’s start by loading network data into R and also load the required igraph libraryigraph is a graph manipulation library that makes it very easy to create, load, analyse, and plot graphs in R. I’ve provided many examples of using igraph but one should really invest some time learning igraph for serious networks and graphs analysis.

```library(igraph) graph <- nexus.get("Davis") plot(graph)``` It is obvious that the plot of the graph is very bland and it isn’t easy to see any natural graph partition. It would be nice to have clusters of people in different colours and shapes. Let’s try to improve the plot by identifying communities in the graph.

## Finding communities

Let’s ignore that this graph is a bipartite graph for now, let’s just try to partition the graph into communities. Is there a natural division? A division of the graph nodes where there are more connections inside the partition than there are connections to other communities? Let’s colour the nodes according to the community they belong:

```graph.com <- fastgreedy.community(graph) V(graph)\$color <- graph.com\$membership + 1 plot(graph)```

To find the communities in the graph, we first use Clauset et al2 greedy algorithm that maximises modularity of the graph in an agglomerative hierarchical clustering. Modularity compares the density of connections inside communities with a null model where connections between graph nodes was random. Then we assign the attribute color according to the graph community membership. ## Change graph node’s shape

As we see the algorithm found 2 communities and at first inspection the division seams reasonable. In any case remember that there's another natural division that the algorithm couldn't find: the bipartite relation Event / Woman. Each node has this other property that says “Is Woman” or “Is Event”. Let's use that to characterise the graph in a different way. We have 14 Events. For these we'll change their shape.

```V(graph)[V(graph)\$type == 1]\$shape <- "square" V(graph)[V(graph)\$type == 0]\$shape <- "circle" plot(graph)``` As we can see, we change the look of the graph in R by changing attributes of the nodes or of the edges. Almost every visual aspect can be changed, from the layout of the graph, to the size of the elements of the graph. This examples illustrated the basic mechanisms to change the plotting of graphs to make them more informative.

1. Allison Davis, Burleigh Bradford Gardner, Mary R. Gardner, Deep south: a social anthropological study of caste and class, University of Chicago Press (1941).

2. A Clauset, MEJ Newman, C Moore: Finding community structure in very large networks, http://www.arxiv.org/abs/cond-mat/0408187