3
$\begingroup$

In the following graph I want:

(1) the origin, vertex 1, to be on top;

(2) the ends to be at the same line at the bottom (two are now at a higher level);

(3) to specify that one vertex (vertex 6) should be on the same level as others (vertexes 2, 10, and 11).

Graph[{1 -> 2, 1 -> 3, 2 -> 4, 2 -> 5, 3 -> 6, 3 -> 7, 6 -> 8, 6 -> 9,
   7 -> 10, 7 -> 11, 10 -> 12, 10 -> 13, 11 -> 14, 11 -> 15},
 VertexLabels -> {1 -> Placed["Alt?", Center], 
   2 -> Placed[ "One flse postve?", Center], 
   6 -> Placed[ "One flse postve?", Center], 
   3 -> Placed["In database?", Center],
   7 -> Placed["True Positive?", Center], 
   10 -> Placed[ "One flse postve?", Center], 
   11 -> Placed["More positives?", Center],
   4 -> Placed[ "False positive", Center], 
   8 -> Placed[ "False positive", Center], 
   15 -> Placed[ "True positive", Center], 
   12 -> Placed[ "True positive", Center]},
 EdgeLabels -> {1 \[DirectedEdge] 2 -> "Yes", 
   1 \[DirectedEdge] 3 -> "No", 3 \[DirectedEdge] 6 -> "No", 
   3 \[DirectedEdge] 7 -> "Yes", 2 \[DirectedEdge] 4 -> "Yes", 
   2 \[DirectedEdge] 5 -> "No", 7 \[DirectedEdge] 10 -> "No", 
   7 \[DirectedEdge] 11 -> "Yes",
   6 \[DirectedEdge] 8 -> "Yes", 6 \[DirectedEdge] 9 -> "No", 
   10 \[DirectedEdge] 12 -> "Yes", 10 \[DirectedEdge] 13 -> "No", 
   11 \[DirectedEdge] 14 -> "Yes", 11 \[DirectedEdge] 15 -> "No"},
  VertexShapeFunction -> "Square", VertexSize -> {.38, .1}]

but now it looks like this, with the origin to the left and the ends on different levels:

enter image description here

$\endgroup$

1 Answer 1

3
$\begingroup$

Using the option GraphLayout->{"LayeredEmbedding","RootVertex" -> 1} gives a tree with root vertex at node 1.

edgelist = {1 -> 2, 1 -> 3, 2 -> 4, 2 -> 5, 3 -> 6, 3 -> 7, 6 -> 8, 
  6 -> 9, 7 -> 10, 7 -> 11, 10 -> 12, 10 -> 13, 11 -> 14, 11 -> 15}; 

vlabels = {1 -> Placed["Alt?", Center], 
   2 -> Placed["One flse postve?", Center], 
   6 -> Placed["One false postve?", Center], 
   3 -> Placed["In database?", Center], 
   7 -> Placed["True Positive?", Center], 
   10 -> Placed["One flse postve?", Center], 
   11 -> Placed["More positives?", Center], 
   4 -> Placed["False positive", Center], 
   8 -> Placed["False positive", Center], 
   15 -> Placed["True positive", Center], 
   12 -> Placed["True positive", Center]};
elabels = {(1 -> 2) -> "Yes", (1 -> 3) -> "No", (3 -> 6) -> 
    "No", (3 -> 7) -> "Yes", (2 -> 4) -> "Yes", (2 -> 5) -> 
    "No", (7 -> 10) -> "No", (7 -> 11) -> "Yes", (6 -> 8) -> 
    "Yes", (6 -> 9) -> "No", (10 -> 12) -> "Yes", (10 -> 13) -> 
    "No", (11 -> 14) -> "Yes", (11 -> 15) -> "No"};

options = Sequence[GraphLayout -> {"LayeredEmbedding", "RootVertex" -> 1, 
    LayerSizeFunction -> (1 &), "LeafDistance" -> 2}, 
  VertexLabels -> vlabels, EdgeLabels -> elabels, 
  VertexShapeFunction -> "Rectangle", VertexSize -> {.2, .1}];
g0 = Graph[edgelist, options]

Mathematica graphics

For post-processing g0 define a function vcF to change the corrdinates of selected nodes:

ClearAll[vcF]
vcF = #2 -> Property[#2, VertexCoordinates -> {PropertyValue[{#, #2}, 
        VertexCoordinates][[1]], PropertyValue[{#, #3}, VertexCoordinates][[2]]}] &; 

Graph[ VertexList[g0] /. {vcF[g0, 2, 10], vcF[g0, 6, 10], vcF[g0, 9, 15], 
   vcF[g0, 8, 15], vcF[g0, 5, 15], vcF[g0, 4, 15]}, edgelist, options]

Mathematica graphics

Update: Another partial solution is to use the setting "MultipartiteEmbedding" for the option GraphLayout combined with a vertex list in a specific order:

Graph[{1, 3, 7, 2, 6, 10, 11, 4, 5, 8, 9, 12, 13, 14, 15}, edgelist,
 GraphLayout -> {"MultipartiteEmbedding", "VertexPartition" -> {1, 1, 1, 4, 8}}, 
 VertexLabels -> vlabels, EdgeLabels -> elabels, 
 VertexShapeFunction -> "Square", VertexSize -> {.3, .1}] 

Mathematica graphics

Update 2: You can also specify the vertex coordinates manually:

Graph[edgelist, VertexLabels -> vlabels, EdgeLabels -> elabels, 
 VertexShapeFunction -> "Rectangle", VertexSize -> {.6, .2}, 
 VertexCoordinates -> {4 -> {1, 0}, 5 -> {3, 0}, 8 -> {5, 0}, 
   9 -> {7, 0}, 12 -> {9, 0}, 13 -> {11, 0}, 14 -> {13, 0}, 
   15 -> {15, 0}, 2 -> {2, 1}, 6 -> {6, 1}, 10 -> {10, 1}, 
   11 -> {14, 1}, 7 -> {12, 2}, 3 -> {10, 3}, 1 -> {8, 4}}]

Mathematica graphics

$\endgroup$
2
  • 1
    $\begingroup$ I love the first part, with the "LayeredEmbedding", "RootVertex" -> 1 because it does what asked in a reasonable fashion. The second part, extracting the coordinates and changing them, is enormously complex. The tree graph functions should have a way to tell Mathematica which vertexes to put on each level rather than having to go inside the graph, reverse engineer the coordinates, and manipulate them. As it is, easier to just make the graph using graphics primitives. $\endgroup$
    – Nicholas G
    Commented Nov 20, 2016 at 23:17
  • $\begingroup$ @NicholasG, I agree with you that a more convenient way to get the desired vertex layout should be possible with built-in layout functions. $\endgroup$
    – kglr
    Commented Nov 21, 2016 at 6:32

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.