6
$\begingroup$
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"]

enter image description here

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

$\endgroup$

2 Answers 2

12
$\begingroup$
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 = #}, 
        Thread[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]

enter image description here

Also

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

enter image description here

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

How about

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

instead of

Graph[rule, VertexLabels -> "Name"]

?

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