12
$\begingroup$

I'm new here, so forgive me if this question is not well-posed/duplicates an earlier question - although I've searched for similar questions without success.

I'm trying to present a plot of connections between elements (correlations between student performance on a set of test questions, actually). It's something like (with options omitted):

test = {{1 -> 5, 1}, {4 -> 3, 3.6}, {6 -> 8, 1}, {2 -> 4, 2}, {2 -> 5,
     2.5}, {5 -> 4, 0.9}, {7 -> 8, 2}, {3 -> 8, 1}, {8 -> 2, 1}};
GraphPlot[test, Method -> "CircularEmbedding", VertexLabeling -> True,
  EdgeLabeling -> False]

Mathematica graphics

where the {1,2,3,...} are the labels for my questions.

I want to order the vertices in ascending order (1,2,3,...), i.e. I want vertices with consecutive labels to be adjacent on the circle, like a clock - i.e. as in the image below, which I had to construct by making the connections more or less consecutive:

test2 = {{1 -> 2, 1}, {3 -> 4, 3.6}, {5 -> 6, 1}, {7 -> 8, 
2}, {2 -> 5, 2.5}, {5 -> 4, 0.9}, {7 -> 3, 2}, {3 -> 8, 
1}, {8 -> 2, 1}};
GraphPlot[test2, Method -> "CircularEmbedding", VertexLabeling -> True, EdgeLabeling -> False]

graphics2

Mathematica has its own ideas! It insists on ordering the vertices by the order in which it encounters them in the list of connections, (i.e. 1,5,4,3,...) in the above example. Sorting the list of connections doesn't fix this problem. This issue doesn't seem to be well documented. Any suggestions for a workaround?

Thanks in advance!

$\endgroup$
  • 2
    $\begingroup$ Welcome to Mathematica.SE! Please edit your post to include a working sample of test so the community can fool around with that. $\endgroup$ – Yves Klett Sep 18 '12 at 7:07
  • $\begingroup$ Could you also elaborate a bit on how your result should look like? I do not get it yet - although others may ;-). If you get another upvote you´ll be able to post images etc. as well. $\endgroup$ – Yves Klett Sep 18 '12 at 7:52
  • $\begingroup$ Edited. Thanks again for your help! $\endgroup$ – Ooku Sep 18 '12 at 7:56
  • $\begingroup$ Are disconnected components allowed (this would affect my answer...)? $\endgroup$ – Yves Klett Sep 18 '12 at 8:44
  • $\begingroup$ Yes, disconnected components are allowed - I need it to be robust and general $\endgroup$ – Ooku Sep 18 '12 at 8:46
7
$\begingroup$

One way would be to define VertexCoordinateRules.

First, we extract a sorted list of all your vertices:

myVertices = Sort[Union[Flatten[{test[[All, 1, 1]], test[[All, 1, 2]]}]]];

Then, we use CircularEmbedding from the Combinatorica package to construct a list of equally spaced points on a circle that we'll use for the VertexCoordinateRules:

<< Combinatorica`
coords = Table[myVertices[[i]] -> 
    Partition[Flatten[CircularEmbedding[Length[myVertices]]], 2][[i]], {i, 
    Length[myVertices]}]

And the plot:

GraphPlot[test, VertexLabeling -> True, 
    EdgeLabeling -> False, VertexCoordinateRules -> coords]

enter image description here

A case with disconnected components:

enter image description here

$\endgroup$
8
$\begingroup$

You can get what you need with Graph easily if GraphPlot is not otherwise essential for your application. Like GraphPlot, Graph takes the list of vertices as they appear in the list of edges if a list of vertices is not provided, but, unlike GraphPlot, it also accepts the list of vertices as an argument.

With a few lines to almost replicate the rendering you get in GraphPlot:

(* vertex shape function with labels *)
vsh[{xc_, yc_}, name_, {w_, h_}] := {Red, Rectangle[{xc - w, yc - h},
  {xc + w, yc + h}], Inset[Style[Text[name], 14, White, Bold], {xc, yc}]};
options =  Sequence[VertexSize -> 0.1, VertexShapeFunction -> vsh, 
  GraphLayout -> "CircularEmbedding", ImageSize -> 300];

g1 = Graph[{1 -> 2, 2 -> 5, 3 -> 1, 3 -> 2, 4 -> 5, 4 -> 2}, options];
g2 = Graph[{1, 2, 3, 4, 5}, {1 -> 2, 2 -> 5, 3 -> 1, 3 -> 2, 4 -> 5, 4 -> 2}, options];
g3 = Graph[{5, 1, 4, 3, 2}, {1 -> 2, 2 -> 5, 3 -> 1, 3 -> 2, 4 -> 5, 4 -> 2}, options];
Grid[{{Row[{"VertexList[g1] = ", VertexList[g1]}], 
       Row[{"VertexList[g2] = ", VertexList[g2]}], 
       Row[{"VertexList[g3] = ", VertexList[g3]}]},
       {g1, g2, g3}}, 
       Alignment -> Center]

enter image description here

Update: In case you "have to" use GraphPlot, here is an alternative approach that extracts the vertex coordinates of your graph and remaps the vertices to these coordinates:

options2 = Sequence[Method -> "CircularEmbedding", VertexLabeling -> True, 
       EdgeLabeling -> False, ImageSize -> 300];
vertexlist =  First /@ (test /. Rule -> List) // Flatten // DeleteDuplicates;
(* your original graph *)
gp1 = GraphPlot[test, options2];
(* get the vertex coordinates of gp1 and remap the vertices to these coordinates *)
newvcr = Thread[ Range[Length[vertexlist]] -> 
       First@Cases[gp1, Rule[VertexCoordinateRules, x_] :> x, {0, Infinity}]];
Row[{gp1, GraphPlot[test, options2, VertexCoordinateRules -> newvcr]}, Spacer[5]]

enter image description here

$\endgroup$
5
$\begingroup$

EDIT: Incorporating @VLC´s suggestion concerning disconnected components...

Perhaps this can help you as a starting point (using a supplemented test set):

test = {{1 -> 5, 1}, {4 -> 3, 3.6}, {6 -> 8, 1}, {2 -> 4, 2}, {2 -> 5,
       2.5}, {5 -> 4, 0.9}, {7 -> 8, 2}, {3 -> 8, 1}, {8 -> 2, 
      1}, {9 -> 10, 1}, {13 -> 13, 1}, {17 -> 2, 1}} // Sort // 
   Reverse;

vertices = Union[test[[All, 1]] /. Rule -> List // Flatten ];

coords = MapIndexed[#1 -> {Sin[
       2 Pi (#2[[1]] - 1)/Length[vertices]], 
      Cos[2 Pi (#2[[1]] - 1)/Length[vertices]]} &, vertices];

GraphPlot[test, VertexLabeling -> True, EdgeLabeling -> False, 
 VertexCoordinateRules -> coords]

Mathematica graphics

$\endgroup$
4
$\begingroup$

One approach might be to extract the VertexCoordinateRules from an appropiate GraphPlot (for example from {1->2, 2-> 3, 3-> 4 ...}) using Cases, and use these to order the vertices in the graph of interest.

For example

ceRules[noVertices_, offset_: 0] := 
 MapThread[Rule, {RotateLeft[Range[noVertices], offset], #}] &@
  Cases[GraphPlot[Rule @@@ Partition[Range[noVertices], 2, 1], 
     VertexLabeling -> True, 
     Method -> "CircularEmbedding"], (VertexCoordinateRules -> x_) :> 
     x, Infinity][[1]]

Or, to obtain the vertices in a clockwise order

ceRulesCW[noVertices_, offset_: 0] := 
 MapThread[
    Rule, {Reverse@RotateLeft[Range[noVertices], offset], #}] &@
  Cases[GraphPlot[Rule @@@ Partition[Range[noVertices], 2, 1], 
     VertexLabeling -> True, 
     Method -> "CircularEmbedding"], (VertexCoordinateRules -> x_) :> 
     x, Infinity][[1]]

With

test = {{1 -> 5, 1}, {4 -> 3, 3.6}, {6 -> 8, 1}, {2 -> 4, 2}, {2 -> 5,
     2.5}, {5 -> 4, 0.9}, {7 -> 8, 2}, {3 -> 8, 1}, {8 -> 2, 1}};

the following may be obtained:

Grid@{GraphPlot[test, VertexLabeling -> True, EdgeLabeling -> False, 
     VertexCoordinateRules -> #] & /@ {ceRules[8, -4], 
    ceRulesCW[8, -3]}}

Two GraphPlots with vertices numbered clockwise & anticlockwise

For example:

ceRules@10

{1 -> {3.23607, 1.53884}, 2 -> {2.92705, 2.4899}, 3 -> {2.11803, 3.07768}, 4 -> {1.11803, 3.07768},

5 -> {0.309017, 2.4899}, 6 -> {0., 1.53884}, 7 -> {0.309017, 0.587785},

8 -> {1.11803, 1.3469*10^-10}, 9 -> {2.11803, 0.}, 10 -> {2.92705, 0.587785}}

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