I have several large two-layer directed graphs with vertex labels of unequal length, and I'd like to lay them out and color vertices in an automatic way, as I'll describe.
Here is a minimum example and some of the component functions I've been exploring:
myEdges = {1 -> 2, 1 -> 5, 3 -> 5, 4 -> 5};
g = Graph[myEdges];
myVertexNames = {"aa", "bbbbbbb", "ccccc", "dddd", "ee"};
I want the graph to be displayed with the sources aligned vertically on the left, and the sinks (leafs) to be aligned vertically at the right, with the labels in each vertex in proper horizontal reading orientation. I've managed to achieve the orientation of text and graph by
Rotate[Graph[g,
VertexLabels ->
Table[i ->
Placed[Rotate[
Text[Style[ToString[myVertexNames[[i]]], 12]], -\[Pi]/2],
Center, Panel], {i, Length[myVertexNames]}]], \[Pi]/2]
(I suspect there is a more elegant approach here, something with a graph orientation being Left.)
As will become clear, we'll need to distinguish between the input vertices and the output vertices, whose identities can be computed by:
theSources = Flatten[Position[VertexOutDegree[g], _?Positive]];
theSinks = Flatten[Position[VertexInDegree[g], _?Positive]];
All of my graphs will have just sources (with possibly different outdegrees) and sinks (with possibly different indegrees), as in the example graph g.
Now for the tricky parts.
First, as the figure shows, the arrowheads are lost (hidden behind the panels), but I'd like them to be visible and go right to the edge of the bounding boxes of the panels. This seems particularly tricky because the panels will be of different size (because the vertex labels are of different length).
Second, because the vertex label lengths differ, I'd like the labels in theSources (in the left column) to be right justified in each panel, and the labels in theSinks (on the right) to be left justified in each panel. (The VertexShape should be rectangular, but of dimensions that just bound the labels with a small margin.) More specifically, I'd like the right sides of all the bounding boxes for theSources be aligned precisely vertically, and the left side of all the bounding boxes for theSinks should align precisely vertically. (The overall graph presentation will be "ragged" on the outer left, and the outer right, because of the unequal lengths of vertex labels... which is fine.)
Finally, I'd like the background colors of theSource vertices to differ from that of theSinks colors (for instance Red and Green).
I can hand kludge a solution for a single network, but have had difficulty performing all this algorithmically. I'm also hoping to stay with Graph (not GraphPlot) for the default style and layout options.
Some progress using LayeredGraphPlot:
LayeredGraphPlot[
myNewEdges, Left,
VertexRenderingFunction -> (Text[Framed[Style[#2, 8],
Background -> If[MemberQ[thesources, #3], Red, Yellow]], #1] &)]
I'm getting closer, but still can't align the boxes as I need: