# Display several graphs with shared vertices?

Consider the two graphs:

gr1 = Graph[{DirectedEdge[1, 2], DirectedEdge[3, 2]}, EdgeStyle -> Black, VertexLabels -> Table[i -> Subscript[x, i], {i, 3}]]


gr2 = Graph[{DirectedEdge[4, 1], DirectedEdge[1, 5], DirectedEdge[5, 2], DirectedEdge[2, 6], DirectedEdge[6, 3], DirectedEdge[3, 7]}, EdgeStyle -> Red, VertexLabels -> Table[i -> Subscript[x, i], {i, 3}]]


I am looking for a way to display both graphs together, such that they are connected at the shared vertices x1,x2,x3. Ideally, it should look like:

I tried something like GraphPlot[{gr1,gr2}] but this syntax seems flawed. How should I be doing this?

The union can be computed using GraphUnion. It constructs a new graph containing the vertices and edges from both.

Styling and other properties are lost and need to be re-added.

• This works well, thanks! But is there a way to force the labeled vertices to be on a straight line, while the other ones go in zig-zag? Nov 16 '16 at 18:55
• @Kagaratsch I don't know of any automatic way. You can always set vertex coordinates manually, even copy them from the two graphs. But I don't know how to set only some vertex coordinates and leave the rest to an automatic layout. Nov 16 '16 at 19:05
• @Kagaratsch, maybe GraphUnion[gr1, gr2, VertexLabels -> Table[i -> Subscript[x, i], {i, 3}], GraphLayout -> "MultipartiteEmbedding", EdgeStyle -> Thread[EdgeList[gr1] -> Directive[Thick, Opacity[1], Black]], VertexStyle -> Thread[VertexList[gr1] -> Black], BaseStyle -> Red]?
– kglr
Nov 16 '16 at 22:49
• Wow, this is great, thank you! Nov 16 '16 at 22:53
props = {EdgeStyle -> {_ :> Red,  DirectedEdge[1, 2] | DirectedEdge[3, 2] -> Black},

Note: For this particular case, you can also use EdgeAdd[gr2, EdgeList @ gr1] instead of GraphUnion[gr1, gr2].