How to merge/combine/contract nodes/vertices using igraph in R?
<p>I am rephrasing/reformatting my previous post.</p>

<p>I have a directed network with varied node attributes and edge attributes. Some nodes have exactly same connection. So I want to merge/combine/contract them. I am hoping something like this, the so-called <em>nested networks</em>, but preferrably with arrows.
<a href="http://manual.cytoscape.org/en/stable/Nested_Networks.html" rel="nofollow noreferrer">http://manual.cytoscape.org/en/stable/Nested_Networks.html</a></p>

<p>I found two options which almost do the job. Let me walk through it.</p>

<pre><code>    library(igraph)
    library(dplyr)
    library(RedeR)
    net &lt;- graph_from_data_frame(data.frame(
    from = c("a", "a", "a", "b", "c", "d"),
    to = c("b", "c", "d", "e", "e", "e"),
    type = c("activation","activation","activation",
    "inhibition","inhibition","inhibition")
    ),
    directed = T)
    V(net)$color &lt;- c("grey", "forestgreen", "firebrick", "navyblue", "grey")
    plot(net, layout = layout_as_tree)
    # as you can see, colored nodes, `b,c,d`, are equally connected by `a` and `e`.
</code></pre>

<p>Option 1: <code>igraph::contract</code></p>

<pre><code>net.c &lt;- contract(net, c(1,2,2,2,3), vertex.attr.comb = toString)
# net.c &lt;- simplify(net.c, remove.multiple = T, remove.loops = T, edge.attr.comb = "sum")
# somehow sometimes `simplify` crashed my R, so I did this instead.
net.c &lt;- igraph::as_data_frame(net.c) %&gt;% unique %&gt;% graph_from_data_frame(., directed = T)
# this kept edge attributes while lost vertex color, which is understandable.

# i recreated node color, using pie.shape
plot(net.c,
vertex.shape = c("circle", "pie", "circle"),
vertex.pie = list(c(1,1,1)),
vertex.pie.color = list(c("forestgreen", "firebrick", "navyblue"))
)
</code></pre>

<p>Option 1 may look okay. Edge attributes can be kept. But when the number of merged nodes, here ie. <code>b,c,d</code>, is larger than, say 4, the pie.shaped node is not really readable. Besides, there might also be intraconnnection between those, which can not be shown in a pie.</p>

<p>Option 2: <code>RedeR::nestNodes</code></p>

<pre><code>rdp &lt;- RedPort()
calld(rdp)
resetd(rdp)
addGraph(rdp, net, parent="a")
nestNodes(rdp, nodes = c("b", "c", "d"), nestImage = "plain",
          isAssign = F,
          gscale = 25)
mergeOutEdges(rdp)
</code></pre>

<p><code>b,c,d</code> are contained in a circle, which is very nice. But after <code>mergeOutEdges</code>, arrows outside of container are removed. A trivial thing is that I couldn't figure out how to remove the container label <em>N0</em>, as the argument <code>isAssign</code> didn't seem to work.</p>

<p>This is by far the two options I can find.
Any suggestion how I can improve this from here?
Thank you.</p>