# How to display edge labels above edges?

The following code shows that edge labels "cover" edges in GraphPlot3D

GraphPlot3D[{{1 -> 2, "1\[Rule]2"}, 4 -> 1, {2 -> 4, "2\[Rule]4"},
1 -> 5, 2 -> 5, 5 -> 4},
EdgeRenderingFunction -> ({If[#3 =!= None,
Text[#3, Mean[#1], Background -> Yellow], {}], Line[#]} &)]


But if I change GraphPlot3D to GraphPlot, the edges will go across the labels. How can I keep the labels with the highest z-index? Thank you.

Snapshot

GraphPlot3D

GraphPlot

-

GraphPlot[{{1 -> 2, "1\[Rule]2"}, 4 -> 1, {2 -> 4, "2\[Rule]4"}, 1 -> 5, 2 -> 5, 5 -> 4},
EdgeRenderingFunction ->
({If[#3 =!= None,
{Line[#], Inset[#3, Mean[#1], Automatic, Automatic, #[[1]] - #[[2]],
Background -> White]}, Line[#]]} &)]


-
@belisarius , I understand what you mean: the latter objects will have higher z-index. –  ziyuang Apr 16 '12 at 3:02
@ziyuang I don't know if z-index is the right word, but you got the idea :) –  belisarius Apr 16 '12 at 3:11
+1 however I wonder if Inset obscures the mechanism. Or perhaps I don't understand. See answer below. –  Mr.Wizard Apr 16 '12 at 7:01
@Mr.Wizard Inset[] allows "encapsulated" fine control of the superimposed content. You could (and you did, indeed) go without it if you prefer. –  belisarius Apr 16 '12 at 12:12
@R.M Good! It's never too late :) –  belisarius Apr 16 '12 at 14:55

If you have version 8.0 you might want to use the new Graph object.

In g1 I used Placed, thanks to Belisarius' suggestion, to move the EdgeLabel for 2-> 4 away from the intersection of edges.

g1=Graph[{1 -> 2, 4 -> 1, 2 -> 4, 1 -> 5, 2 -> 5, 5 -> 4},
DirectedEdges -> False, VertexLabels -> "Name",
EdgeLabels -> {(1 -> 2) -> (1 -> 2), (2 -> 4) ->
Placed[(2 -> 4), {.4, {1.25, 3}}]},
EdgeLabelStyle -> Directive[20, Background -> Yellow],


You can avoid the crossing edges altogether with GraphLayout -> "LayeredDrawing":

g2=Graph[{1 -> 2, 4 -> 1, 2 -> 4, 1 -> 5, 2 -> 5, 5 -> 4},
DirectedEdges -> False, VertexLabels -> "Name",
EdgeLabels -> {(1 -> 2) -> (1 -> 2), (2 -> 4) -> (2 -> 4)},
GraphLayout -> "LayeredDrawing",
EdgeLabelStyle -> Directive[20, Background -> Yellow],


...or you can make use of the fact that the graph in case is a CompleteGraph, which has by default a nice radial layout:

g3= CompleteGraph[4, VertexLabels -> "Name", ImagePadding -> 15,
EdgeLabels -> {(1 -> 2) -> (1 -> 2), (2 -> 4) -> (2 -> 4)},
EdgeLabelStyle -> Directive[16, Background -> Yellow]]


Checking...

IsomorphicGraphQ[g1, g2]
IsomorphicGraphQ[g1, g3]

(* Out *)
True
True

-
You can also use EdgeLabels[Placed[] ....] reference.wolfram.com/mathematica/ref/EdgeLabels.html –  belisarius Apr 16 '12 at 14:40
Thanks for the suggestion. I implemented it after some trial and error. –  David Carraher Apr 16 '12 at 16:51

Reordering seems to work without Inset:

GraphPlot[{{1 -> 2, "1\[Rule]2"}, 4 -> 1, {2 -> 4, "2\[Rule]4"},
1 -> 5, 2 -> 5, 5 -> 4},
EdgeRenderingFunction -> ({Line[#],
If[#3 =!= None, Text[#3, Mean[#1], Background -> Yellow], {}]} &)
]


-
What is the difference between using Inset or not? –  ziyuang Apr 16 '12 at 7:18
@ziyuang I think belisarius should answer that as I'm not sure of his intent. –  Mr.Wizard Apr 16 '12 at 7:22