8
$\begingroup$

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

$\endgroup$

4 Answers 4

7
$\begingroup$
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

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

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
$\endgroup$
2
4
$\begingroup$

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

$\endgroup$
2
  • $\begingroup$ What is the difference between using Inset or not? $\endgroup$
    – Ziyuan
    Apr 16, 2012 at 7:18
  • $\begingroup$ @ziyuang I think belisarius should answer that as I'm not sure of his intent. $\endgroup$
    – Mr.Wizard
    Apr 16, 2012 at 7:22
1
$\begingroup$

BezierFunction

GraphPlot[{{1 -> 2, "1->2"}, 4 -> 1, {2 -> 4, "2->4"}, 1 -> 5, 2 -> 5, 5 -> 4}, 
 VertexLabeling -> True, 
 EdgeRenderingFunction -> ({Line[#], 
     If[#3 === None, {}, Text[Framed[Style[#3, 16], Background -> LightRed, 
        FrameStyle -> None], BezierFunction[#][.67], Automatic, Abs[Subtract @@ #]]]} &)]

enter image description here

GraphUtilities`LineScaledCoordinate

Needs["GraphUtilities`"]
GraphPlot[{{1 -> 2, "1->2"}, 4 -> 1, {2 -> 4, "2->4"}, 1 -> 5, 2 -> 5, 5 -> 4}, 
 VertexLabeling -> True,
 EdgeRenderingFunction -> ({Line[#], 
  If[#3 === None, {}, Text[Framed[Style[#3, 16], Background -> LightRed, 
   FrameStyle -> None], LineScaledCoordinate[#, .67], Automatic, Abs[Subtract @@ #]]]} &)]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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