# How to get better labels for this Graph?

PermutationCycles[RandomSample[Range[100], 100]]
creatlistNew[n_] := Block[
{one2onehundred, sample},
one2onehundred = Range[n];
sample = RandomSample[Range[n], n];
Table[one2onehundred[[i]] -> sample[[i]], {i, n}]
];
rule = creatlistNew[100]
Graph[rule, VertexLabels -> "Name"]


The labels seem to be "on top" of some of the vertices. How could I improve this automatically?

radialLabelCoords[rule_, off_ : 3] :=
Module[{cc = ConnectedComponents[Graph[rule]],
vc = Rescale@GraphEmbedding[Graph[Sort@VertexList@rule, rule]]},
Association @ Apply[Join] @
Map[Module[{rc = Mean[vc[[#]]], vl = #},
If[Length@vl == 1, After,
(vc[[#]] + off Normalize[vc[[#]] - rc] & /@ vl)]]] &] @ cc]


Example:

SeedRandom[1];
rule = creatlistNew[100];

Graph[Sort@VertexList@rule, rule,
VertexLabels -> {v_ :> Placed[v, radialLabelCoords[rule]@v]},
ImageSize -> Large]


Also

Graph[rule,
PlotTheme -> "NameLabeled",
PerformanceGoal -> "Quality",
VertexSize -> .6,
ImageSize -> 800]


• **PlotTheme -> "NameLabeled", ** is a nicer way to go! Nov 16, 2023 at 14:41
• Unfortunately, PlotTheme -> "NameLabeled" is not documented. Nov 16, 2023 at 16:05
• It is now documented under Graph > Details and Options
– kglr
Nov 16, 2023 at 16:07

Graph[rule, VertexLabels -> Table[i -> Placed[i, Center], {i, 100}],
VertexSize -> 0.8, VertexShape -> \!$$\*GraphicsBox[CircleBox[{0, 0}],ImageSize->{24., Automatic}]$$, ImageSize -> "Large"]]

Graph[rule, VertexLabels -> "Name"]