TransWikia.com

Equivalent of RadialOutside for Graph VertexLabels

Mathematica Asked by shellhead on April 24, 2021

I’m creating a circular graph and want the labels to be placed on the outside of the vertices in a circular manner on the outside of the graph. I’ve been playing around with the Placed function, but haven’t got very far. I’m looking for results similar to "RadialOutside" that’s available to charts. This seems like it should be easy, but I haven’t been able to get anywhere.

g = Graph[CompleteGraph[26], VertexLabels -> Table[i -> Placed["Name" , Top], {i, 26}]]

4 Answers

g = Graph[CompleteGraph[26], 
VertexLabels -> Table[i -> Placed["Name", {{0,0}, 
{-Cos[Pi/2 + 2 i Pi/26], .25 - Sin[Pi/2 + 2 i Pi/26]}}], {i, 26}]]

enter image description here

Correct answer by Zviovich on April 24, 2021

Not to detract from PatoCriollo's excellent answer, but just to show that there is always a "there is also...".

Furthermore, the following, to my surprise, is not as fragile as I thought it might be with respect changes in ImageSize and in the vertex count of CompleteGraph.

vc = GraphEmbedding[CompleteGraph[26]];
g = Graph[EdgeList@CompleteGraph[26],
  VertexLabelStyle -> Directive[{16, Bold, "Panel"}], ImagePadding -> 20,
  VertexLabels -> Table[i -> Placed["Name", .5 + Pi vc[[i]]], {i, 26}]]

enter image description here

gr = With[{vc = GraphEmbedding[CompleteGraph[#]]},
    Graph[EdgeList@CompleteGraph[#],
     VertexLabelStyle -> Directive[{16, Bold, "Panel"}], 
     ImagePadding -> 20, ImageSize -> #2,
     VertexLabels -> Table[i -> Placed["Name", .5 + Pi vc[[i]]], {i, #}]]] &;

Row[gr[#, 300] & /@ {10, 16, 26}]

enter image description here

Row[gr[16, #] & /@ {200, 300, 500}]

enter image description here

Answered by kglr on April 24, 2021

If you don't mind having a Graphics rather than a Graph object, you can use GraphComputation`GraphPropertyChart which combines a PieChart with the edges of a graph:

GraphComputation`GraphPropertyChart[CompleteGraph[16], 
 ChartStyle -> "Rainbow", 
 ChartBaseStyle -> Directive[EdgeForm[], Opacity[.3]]]

enter image description here

Use the options LabelingFunction -> (Placed[#2[[2]], "VerticalCallout"] &) and ChartLabels -> None to get

enter image description here

Use CompleteGraph[32] as the first argument to get

enter image description here

To remove the annuli, use ChartElementFunction -> None:

 GraphComputation`GraphPropertyChart[CompleteGraph[25], 
  ChartStyle -> "Rainbow", 
  ChartLabels -> Placed[Range[25], "RadialCallout"], 
  ImageSize -> 450, ChartElementFunction -> None, 
  ChartBaseStyle -> Opacity[.5]] /.  _Disk :> {}

enter image description here

Use ChartLabels -> Placed[Range[25], "VerticalCallout"] to get

enter image description here

Answered by kglr on April 24, 2021

I like to use IGVertexMap from IGraph/M to compute label coordinates for circular embeddings.

Here's an example

IGVertexMap[
 Placed["Name", {0.5 + 1.8 Normalize[#], {.5, .5}}] &,
 VertexLabels -> GraphEmbedding,
 IGLayoutCircle[g]
]

enter image description here

This is more concise (and IMO more readable) than any of the other presented solutions. Also, it does not rely on a certain vertex naming, like the accepted answer.

This is how it works:

  • IGLayoutCircle creates a layout centred on {0,0} (unlike "CircularEmbedding").

  • Placed["Name", {pos, epos}] will place the point epos within the label at location pos within the vertex itself. Both are given in scaled coordinates running from 0 to 1.

Here's a more complex example copied from the IGraph/M documentation:

IGVertexMap[
 Function[{name, coord},
  Placed[
   name,
   {{.5, .5}, -0.8 Normalize[coord] + {.5, .5}},
   Rotate[#, Mod[ArcTan @@ coord, Pi, -Pi/2]] &
  ]
 ],
 VertexLabels -> {VertexList, IGVertexProp[VertexCoordinates]},
 IGLayoutCircle@ExampleData[{"NetworkGraph", "FamilyGathering"}]
]

enter image description here

Should you need to order vertices differently along the circle, you can use IGReorderVertices. This function preserves graph properties, which is useful if you want to also style the graph based on some properties with IGVertexMap/IGEdgeMap.

Answered by Szabolcs on April 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP