4
$\begingroup$

I have a tree as below.

Graph[{7 -> 2, 2 -> 3, 3 -> 4, 3 -> 5}, VertexLabels -> "Name"]

enter image description here

However, I want the drawing would put parent nodes at a higher position than their respective children nodes (i.e. 7 should be drawn higher than 2, instead of lower than 2). What can I do to achieve this (I don't want to specify the vertex coordinates manually)? Thanks!

$\endgroup$
5
$\begingroup$

You can use

  1. Graph with the option GraphLayout -> "LayeredDigraphEmbedding", or
  2. LayeredGraphPlot, or
  3. TreePlot specifying the root vertex in the third argument.

 Graph[{7 -> 2, 2 -> 3, 3 -> 4, 3 -> 5}, 
   VertexLabels -> "Name", 
   GraphLayout->"LayeredDigraphEmbedding"]

enter image description here

LayeredGraphPlot[{7 -> 2, 2 -> 3, 3 -> 4, 3 -> 5}, 
   VertexLabels -> "Name"]

same picture

TreePlot[{7 -> 2, 2 -> 3, 3 -> 4, 3 -> 5}, Automatic, 7, 
 VertexLabels-> "Name", DirectedEdges -> True]

enter image description here

Note: LayeredGraphPlot and TreePlot produce Graphics objects.

$\endgroup$
2
$\begingroup$
Graph[{7 -> 2, 2 -> 3, 3 -> 4, 3 -> 5}, VertexLabels -> "Name", 
 GraphLayout -> {"LayeredEmbedding", "RootVertex" -> 7}]

enter image description here

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