4
$\begingroup$

Edge labels are always rendered horizontally. I would like the label for any straight line edge to be rotated so the bottom of the label's bounding box is coincident with the edge.

There was an older post (Comfortable Edge Labeling of Undirected Graph) that did this using EdgeRenderingFunction, but that has been superseded by EdgeShapeFunction in v12. EdgeShapeFunction does not have access to the label like EdgeRenderingFunction did.

Labels following a curved edge would be a bonus, but just paralleling a tangent somewhere on the edge would suffice.

$\endgroup$
1
  • 1
    $\begingroup$ Take a look at this. $\endgroup$ Commented Feb 13, 2020 at 2:30

1 Answer 1

3
$\begingroup$

You can get the old GraphPlot function in version 12.0.0 using GraphComputation`GraphPlotLegacy.

Using a modification of the function erf from this answer:

ClearAll[erf];
erf[pos_List: {.5}, lblcolors_: {Black}, fontsizes_: {12}, 
    linecolor_: Directive[Opacity[0.7], Hue[0.6, 0.7, 0.5]]] := 
   {linecolor, Line[#], 
     Sequence @@ Table[Inset[Style[Text[#3], lblcolors[[i]], 
       FontSize -> fontsizes[[i]]],  
     pos[[i]] First@# + (1 - pos[[i]]) Last@#, {Center, Bottom}, 
     Automatic, -Subtract @@ Sort[#], Background -> None], 
    {i, 1, Length@pos}]} &

Examples:

edges = {{4 -> 2}, {5 -> 1}, {3 -> 2}, {2 -> 5}, {3 -> 5}};
labelededges = MapIndexed[{First@#1, ToString@First@#2} &, edges];

GraphComputation`GraphPlotLegacy[labelededges, 
 EdgeRenderingFunction -> erf[], 
 PlotStyle -> Thick, VertexLabeling -> True, 
 ImagePadding -> 20, ImageSize -> 500]

enter image description here

SeedRandom[1]
labelededges2 = Transpose[{Flatten@edges, StringTake[ RandomWord[5], 4]}];

GraphComputation`GraphPlotLegacy[labelededges2, 
 VertexLabeling -> True, 
 EdgeRenderingFunction -> erf[], ImageSize -> 500]

enter image description here

$\endgroup$
1
  • $\begingroup$ I've marked this as a solution, but I actually prefer the answer you gave in the link posted by @Rohit in the comment attached to my original question for three reasons: 1) it uses current, not deprecated, functions, 2) it shows the rendering along a curve, 3) it adds the bonus of multiple labels. $\endgroup$
    – G. Shults
    Commented Feb 13, 2020 at 18:11

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.