4

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.

G. Shults
  • 413
  • 2
  • 10

1 Answers1

3

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

kglr
  • 394,356
  • 18
  • 477
  • 896
  • 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. – G. Shults Feb 13 '20 at 18:11