7
$\begingroup$

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}]]
$\endgroup$
8
$\begingroup$
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

$\endgroup$
  • $\begingroup$ (++1) Nice!!!!! $\endgroup$ – kglr Oct 18 '14 at 3:12
  • $\begingroup$ Great! Thank you so much! $\endgroup$ – shellhead Oct 18 '14 at 3:16
  • 1
    $\begingroup$ (+1) but with ImageSize -> 900 , labels are one the vertices $\endgroup$ – Algohi Oct 18 '14 at 3:20
8
$\begingroup$

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

$\endgroup$
5
$\begingroup$

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

$\endgroup$
  • $\begingroup$ How'd you find this one? It's very cool. Also presumably the layout info from here could be reinserted into a Graph object to get a Graph in the end. $\endgroup$ – b3m2a1 Jul 22 '18 at 21:44
  • $\begingroup$ @b3m2a1, i was browsing through the functions in ``??GraphComputation```. The name sounded interesting and it worked in my first try with a graph input. $\endgroup$ – kglr Jul 22 '18 at 21:50
3
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.