# In Graph, can text and graphics be “layered”

I have constructed a small tool to produce documentation for my code. It constructs dataflow diagrams (my own version) using an association-based data structure and Graph.

The DFDs look pretty good except that some of the labeling gets buried behind some of the vertices, and I haven't been able to figure out how to make them layer as I wish. Take a look at the vertex "buildPartnerGraphLF" which is on top of the edge label.

As background, the association data structure gets translated into the lists below which are then fed to Graph. I include the raw lists. Any help would be appreciated.

 vL = {Property["buildLimitedPGraphLF", VertexShapeFunction -> "Circle"],
Property["buildPartnerGraphLF", {VertexShapeFunction -> "Circle",
VertexStyle -> RGBColor[0.87, 0.94, 1], VertexSize -> {0.3, 0.3}}],
Property["fullPartL", VertexShapeFunction -> "RoundedRectangle"],
Property["seedPartnerL", VertexShapeFunction -> "RoundedRectangle"],
Property["shortSeedPL", VertexShapeFunction -> "RoundedRectangle"]}

eL = {"buildLimitedPGraphLF" \[DirectedEdge] "buildPartnerGraphLF",
"buildLimitedPGraphLF" \[DirectedEdge] "fullPartL",
"buildPartnerGraphLF" \[DirectedEdge] "getPartnerF",
"buildPartnerGraphLF" \[DirectedEdge] "seedPartnerL",
"frequentPartF" \[DirectedEdge] "shortSeedPL",
"getPartnerF" \[DirectedEdge] "buildPartnerGraphLF",
"medF" \[DirectedEdge] "frequentPartF",
"medF" \[DirectedEdge] "getPartTimesF",
"seedPartnerL" \[DirectedEdge] "frequentPartF",
"shortSeedPL" \[DirectedEdge] "buildLimitedPGraphLF"}

eLabL={"buildLimitedPGraphLF" \[DirectedEdge] "buildPartnerGraphLF" ->
"brokerS medI",
"buildLimitedPGraphLF" \[DirectedEdge] "fullPartL" ->
"broker+partnerL",
"buildPartnerGraphLF" \[DirectedEdge] "getPartnerF" ->
"brokerS", "dealDataDS" \[DirectedEdge] "getPartnerF" -> "partnerL",
"getPartnerF" \[DirectedEdge] "buildPartnerGraphLF" -> "partnerL",
"leadOrCoL", "medF" \[DirectedEdge] "frequentPartF" -> "medI",
"medF" \[DirectedEdge] "getPartTimesF" -> "brokerS",
"shortSeedPL" \[DirectedEdge] "buildLimitedPGraphLF" ->
"Each/partnerS"}

g1 = Graph[vL, eL, EdgeLabels -> eLabL,
VertexLabels -> Placed["Name", Center], VertexSize -> {0.25, .25},
ImageSize -> Full]


• Use as option a color with opacity VertexStyle -> RGBColor[0.77, 0.76, 0.21, .3] would be a quick way to see all labeling. – Rolf Mertig Dec 22 '16 at 21:11

You can move all of the text to the top with

Show[Show@g1 /. {{_Text -> {}}, {_Disk | _Rectangle | _Arrow | _FilledCurve -> {}}}]


This uses Show to convert the graph to a normal graphics object, and then uses Show again to layer a copy with only the text over a copy with all of the text removed.

• I had to read that replacement three times to see what you are doing. That's clever! – Mr.Wizard Dec 23 '16 at 2:34
• @Mr.Wizard Thanks! I think that's the first time I've made good use of the "list-of-lists" syntax for ReplaceAll. – Simon Rochester Dec 23 '16 at 3:14
• Awesome is the word that suggests itself to me! – Mike Colacino Dec 23 '16 at 17:23
• For those reading this, @Simon Rochester has (in one line) converted the graph to a graphic, then created two versions of the graphic, using replacement rules to turn "off" all of the text in the first and all of the other graphics in the second. Then Show essentially overlays the text-y one on top of the graphic-y one. Thanks! – Mike Colacino Dec 23 '16 at 17:53
• @mdc18550 You're welcome. I just found a way to shorten it some more -- you can use Show instead of FullGraphics to convert the graph object to graphics; that removes the need to specify ImageSize again. – Simon Rochester Dec 23 '16 at 23:42

An approach to get a Graph object as the final output:

1. Make the labels in g1 invisible.
2. Extract the Insets and InsetBoxes that contain the vertex and edge labels.
3. Use the extracted labels as Epilog using SetProperty.

Example:

g2 = SetProperty[g1, Thread[{EdgeLabelStyle, VertexLabelStyle} -> Opacity[0]]];
labels = Cases[Show@g1 , i : (Inset | InsetBox)[__] :> i, {0,  ∞}];
g3 = SetProperty[g2, Epilog -> labels]


Head @ g3


Graph

Alternatively, do all in a single step:

SetProperty[SetProperty[g1, Thread[{EdgeLabelStyle, VertexLabelStyle} -> Opacity[0]]],
Epilog -> Cases[Show@g1 , i : (Inset | InsetBox)[__] :> i, {0, ∞}]]


same picture