# How to make a plot that groups different type of nodes by cluster?

**URL:** <https://igraph.discourse.group/t/how-to-make-a-plot-that-groups-different-type-of-nodes-by-cluster/425>\
**Category:** Usage\
**Tags:** R\
**Created:** [28 August 2020 22:02 UTC](https://igraph.discourse.group/t/how-to-make-a-plot-that-groups-different-type-of-nodes-by-cluster/425 "2020-08-28T22:02:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Madhu](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@Madhu](https://igraph.discourse.group/u/Madhu)\
**Post date:** [28 August 2020 22:02 UTC](https://igraph.discourse.group/t/how-to-make-a-plot-that-groups-different-type-of-nodes-by-cluster/425/1 "2020-08-28T22:02:55Z")

</div>

I have a dataframe `df` with nodes information like below:

```
dput(df)

structure(list(Names = c("A4GALT", "AASS", "ABCA10", "ABCA7", 
"ABCD4", "ABHD4", "ABTB1", "AC006978.2", "AC009119.2"), type = c("typeA", 
"typeA", "typeC", "typeA", "typeC", "typeC", "typeB", "typeB", 
"typeB"), type_num = c(1L, 1L, 3L, 1L, 3L, 3L, 2L, 2L, 2L), Clusters = c("Cluster1", 
"Cluster1", "Cluster2", "Cluster3", "Cluster3", "Cluster1", "Cluster2", 
"Cluster3", "Cluster2")), row.names = c(NA, 9L), class = "data.frame") 

```

So, in the `df` dataframe, there are 4 columns. `Names` is the gene name, `type` is different types A, B and C, `type_num` is the number given to each type and column `Clusters` show 3 clusters in which each gene belong to.

Similarly, I have other dataframe `df2` with with edges information. And below I’m showing that:

```
dput(df2)

structure(list(fromNode = c("A4GALT", "A4GALT", "A4GALT", "A4GALT", 
"A4GALT", "A4GALT", "A4GALT", "A4GALT", "AASS", "AASS", "AASS", 
"AASS", "AASS", "AASS", "AASS", "ABCA10", "ABCA10", "ABCA10", 
"ABCA10", "ABCA10", "ABCA10", "ABCA7", "ABCA7", "ABCA7", "ABCA7", 
"ABCA7", "ABCD4", "ABCD4", "ABCD4", "ABCD4", "ABHD4", "ABHD4", 
"ABHD4", "ABTB1", "ABTB1", "AC006978.2"), toNode = c("AASS", 
"ABCA10", "ABCA7", "ABCD4", "ABHD4", "ABTB1", "AC006978.2", "AC009119.2", 
"ABCA10", "ABCA7", "ABCD4", "ABHD4", "ABTB1", "AC006978.2", "AC009119.2", 
"ABCA7", "ABCD4", "ABHD4", "ABTB1", "AC006978.2", "AC009119.2", 
"ABCD4", "ABHD4", "ABTB1", "AC006978.2", "AC009119.2", "ABHD4", 
"ABTB1", "AC006978.2", "AC009119.2", "ABTB1", "AC006978.2", "AC009119.2", 
"AC006978.2", "AC009119.2", "AC009119.2"), weight = c(0.005842835, 
0.002253695, 0.014513253, 0.004851739, 0.066702792, 0.009418991, 
0.001136938, 0.000474221, 0.004405601, 0.000666001, 0.005625977, 
0.0333554, 0.004666223, 0.000103131, 0.00026302, 0.004514819, 
0.029632695, 0.001825839, 0.028379806, 0.001403298, 0.008339397, 
0.02393394, 0.004782329, 0.024767355, 0.002986813, 0.00559471, 
0.005961539, 0.064831874, 0.013023138, 0.027935729, 0.006618816, 
0.001134219, 0.012798368, 0.007961242, 0.01640476, 0.007997743
), direction = c("undirected", "undirected", "undirected", "undirected", 
"undirected", "undirected", "undirected", "undirected", "undirected", 
"undirected", "undirected", "undirected", "undirected", "undirected", 
"undirected", "undirected", "undirected", "undirected", "undirected", 
"undirected", "undirected", "undirected", "undirected", "undirected", 
"undirected", "undirected", "undirected", "undirected", "undirected", 
"undirected", "undirected", "undirected", "undirected", "undirected", 
"undirected", "undirected")), row.names = c(NA, -36L), class = "data.frame")

```

I wanted to create a network like below which represents the clusters with types having different colors in each cluster. It should look something like this:

 ![Screen Shot 2020-08-28 at 23.53.37](https://global.discourse-cdn.com/free1/uploads/igraph/original/1X/c196b9c5c4c6dac24b8723dcbcaa05112354e6af.jpeg)

In the above image the numbers represent different clusters and colors represent different types which belong to each cluster and connected.

Can anyone please help me how to create a network like above to collapse each cluster into a single network node and visualize a network with clusters as nodes. Any help is appreciated. thank you in advance.

---

<div class="post-metadata">

**Author:** ![vtraag](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/vtraag/32/38_2.png) [@vtraag](https://igraph.discourse.group/u/vtraag)\
**Post date:** [25 September 2020 10:48 UTC](https://igraph.discourse.group/t/how-to-make-a-plot-that-groups-different-type-of-nodes-by-cluster/425/2 "2020-09-25T10:48:58Z")

</div>

To generate the network you can use [`graph_from_data_frame`](https://www.rdocumentation.org/packages/igraph/versions/1.2.5/topics/as_data_frame).

You can cluster the graph with several different algorithms, e.g. using [`cluster_infomap`](https://www.rdocumentation.org/packages/igraph/versions/1.2.5/topics/cluster_infomap).

You can collapse each cluster in a single network node by using [`contract`](https://www.rdocumentation.org/packages/igraph/versions/1.2.5/topics/contract). You can simply provide the [`membership`](https://www.rdocumentation.org/packages/igraph/versions/1.2.5/topics/membership) vector of the resulting clustering. You probably also want to simplify.

```auto
H <- contract(G, membership(communities))
H <- simplify(H, remove.loops=FALSE)

```

You can then simply plot the resulting graph `H`.

---

<div class="post-metadata">

**Author:** ![Madhu](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@Madhu](https://igraph.discourse.group/u/Madhu)\
**Post date:** [25 September 2020 11:36 UTC](https://igraph.discourse.group/t/how-to-make-a-plot-that-groups-different-type-of-nodes-by-cluster/425/3 "2020-09-25T11:36:11Z")

</div>

Thank you for your reply. What is the object G here. Which data did you take?

And I tried doing the network but it didn’t look good and I wanted to increase the distance between the clusters here [[How to increase the distance between the clusters using igraph?](https://igraph.discourse.group/t/how-to-increase-the-distance-between-the-clusters-using-igraph/459)]

---

<div class="post-metadata">

**Author:** ![vtraag](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/vtraag/32/38_2.png) [@vtraag](https://igraph.discourse.group/u/vtraag)\
**Post date:** [25 September 2020 11:40 UTC](https://igraph.discourse.group/t/how-to-make-a-plot-that-groups-different-type-of-nodes-by-cluster/425/4 "2020-09-25T11:40:58Z")

</div>

> [@Madhu](#):
>
> What is the object G here.

This is the graph object, please see the [documentation](https://www.rdocumentation.org/packages/igraph/versions/1.2.5/topics/contract). I have not used any data, this is just an example. You should construct the graph `G` yourself by using `graph_from_data_frame`, as said.
