I would like to draw a graph in circular layout but I would like to position the vertices in descending order on the circle. Can this be done in an easy way?
3 Answers
Sort the vertices first. Here is an example graph:
SeedRandom[1]
g = RandomGraph[{20,40}]
We can use DegreeCentrality
to get all of the degrees:
DegreeCentrality[g]
{5, 4, 5, 4, 6, 2, 4, 3, 1, 4, 4, 3, 5, 4, 5, 2, 4, 3, 5, 7}
So, the following should do what you want:
Graph[
VertexList[g][[Ordering[DegreeCentrality[g]]]],
EdgeList[g],
GraphLayout->"CircularEmbedding"
]
-
$\begingroup$ Any reason you chose
DegreeCentrality
overVertexDegree
? $\endgroup$ Commented Jul 23, 2018 at 5:21
Suppose you already have some graph properties (such as edge or vertex weights) or some graph styling. The IGReorderVertices
convenience function from IGraph/M makes it easy to preserve these properties during reordering.
Here's a styled graph:
g = RandomGraph[BarabasiAlbertGraphDistribution[20, 3], VertexSize -> Large, EdgeStyle -> Gray] //
IGVertexMap[ColorData["SolarColors"], VertexStyle -> Rescale@*VertexDegree]
And here it is with circular layout:
IGReorderVertices[
VertexList[g][[ Ordering@VertexDegree[g] ]],
Graph[g, GraphLayout -> "CircularEmbedding"]
]
GraphPropertyChart
GraphComputation`GraphPropertyChart
is an alternative way to visualize vertex degree centralities using a circular layout.
Using Carl Wolls' example:
SeedRandom[1]
g = RandomGraph[{20, 40}];
dc = DegreeCentrality[g];
ordering = Ordering[dc];
GraphComputation`GraphPropertyChart[g, Automatic -> dc[[ordering]],
ChartLabels -> {Placed[{dc[[ordering]], ordering}, {"RadialCenter", "RadialCallout"}]},
ChartStyle -> "SolarColors"] /. False -> True
Or, remove the annuli and disks from the callout lines:
GraphComputation`GraphPropertyChart[g, Automatic -> dc[[ordering]],
ChartElementFunction -> None,
ChartLabels -> {Placed[{dc[[ordering]], ordering}, {"RadialCenter", "RadialCallout"}]},
ChartStyle -> "SolarColors"] /. False -> True /. _Disk:> {}
To order vertices by a property other than the default DegreeCentrality
, say BetweennessCentrality
, we use the desired ordering as the value of the option Automatic
:
bc = BetweennessCentrality[g];
GraphComputation`GraphPropertyChart[g,
Automatic -> Sort[bc],
ChartStyle -> "Rainbow",
ChartLabels -> {Placed[{Sort@bc, Ordering@bc},
{"RadialCenter", "RadialCallout"}]}] /. _Disk :> {}