Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

enter image description here

GraphPlot

enter image description here

share|improve this question
add comment

3 Answers 3

up vote 6 down vote accepted
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[#]]} &)]

enter image description here

share|improve this answer
    
@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
add comment

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], 
       ImagePadding -> 15]

graph


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], 
      ImagePadding -> 15]

graph2


...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]]

graph3


Checking...

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

(* Out *)
True
True
share|improve this answer
    
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
add comment

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], {}]} &)
]

Mathematica graphics

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.