<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/visualizing-graphs-with-overlapping-node-groups/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://datascience.blog.wzb.eu/2018/05/11/visualizing-graphs-with-overlapping-node-groups/"> r-bloggers – WZB Data Science Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I recently came across some data about multilateral agreements, which needed to be visualized as network plots. This data had some peculiarities that made it more difficult to create a plot that was easy to understand. First, the nodes in the graph were organized in groups but each node could belong to multiple groups or to no group at all. Second, there was one “super node” that was connected to all other nodes (while “normal” nodes were only connected within their group). This made it difficult to find the right layout that showed the connections between the nodes as well as the group memberships. However, digging a little deeper into the R packages <a rel="nofollow" href="http://igraph.org/r/" target="_blank">igraph</a> and <a rel="nofollow" href="https://cran.r-project.org/web/packages/ggraph/index.html" target="_blank">ggraph</a> it is possible to get satisfying results in such a scenario.</p>
<p><span></span></p>
<h3>Example data, nodes &amp; edges</h3>
<p>We will use the following packages that we need to load at first:</p>
<pre><code>library(dplyr)
library(purrr)
library(igraph)
library(ggplot2)
library(ggraph)
library(RColorBrewer)
</code></pre>
<p>Let’s create some exemplary data. Let’s say we have 4 groups <em>a, b, c, d</em> and 40 nodes with the node IDs 1 to 40. Each node can belong to several groups but it must not belong to any group. An example would be the following data:</p>
<pre><code>group_a &lt;- 1:5            # nodes 1 to 5 in group a
group_b &lt;- 1:10           # nodes 1 to 10 in group b
group_c &lt;- c(1:3, 7:18)   # nodes 1 to 3 and 7 to 18 in c
group_d &lt;- c(1:4, 15:25)  # nodes 1 to 4 and 15 to 25 in d

members &lt;- data_frame(id = c(group_a, group_b, group_c, group_d, 26:40),
                      group = c(rep('a', length(group_a)),
                                rep('b', length(group_b)),
                                rep('c', length(group_c)),
                                rep('d', length(group_d)),
                                rep(NA, 15)))   # nodes 26 to 40 do not
                                                # belong to any group
</code></pre>
<p>An excerpt of the data:</p>
<pre><code>> members
     id group
  </code></pre>