10
$\begingroup$

How to set the options or other parameters in a TreePlot to draw the branching outside of the vertex? I.e. in a form similar to a dendrogram (but the dendrogram plot itself does not support the vertices).

Some as an example in the figure

enter image description here

$\endgroup$
9
$\begingroup$
ClearAll[zigzag]
zigzag[][{{a_, b_}, {c_, d_}}, ___] := Line @ 
   {{a, b}, {(a + c)/2, b}, {(a + c)/2, d}, {c, d}}
zigzag["Vertical"][{{a_, b_}, {c_, d_}}, rest___] := Map[Reverse, 
  zigzag[][{{b, a}, {d, c}}, rest], {2}]

Examples:

vlabels = "v" <> ToString @ # & /@ Range[5]
edges = {1 -> 2, 1 -> 3, 3 -> 4, 3 -> 5};

Graph[Range[5], edges,  
  VertexLabels -> {v_ :> Placed[vlabels[[v]], Center]}, 
  VertexStyle -> White, 
  VertexLabelStyle -> 16,
  VertexShapeFunction -> "Square", 
  VertexSize -> Large, 
  EdgeStyle -> Directive[Thick, Black], 
  EdgeShapeFunction -> zigzag[], 
  GraphLayout -> {"LayeredEmbedding", "Orientation" -> Left}]

enter image description here

With "Orientation" -> Right we get

enter image description here

For Top and Bottom orientations use zigzag["Vertical"]:

Graph[Range[5], edges,  
 VertexLabels -> {v_ :> Placed[vlabels[[v]], Center]}, 
 VertexStyle -> White, 
 VertexLabelStyle -> 16,
 VertexShapeFunction -> "Square", 
 VertexSize -> Large, EdgeStyle -> Directive[Thick, Black], 
 EdgeShapeFunction -> zigzag["Vertical"], 
 GraphLayout -> {"LayeredEmbedding", "Orientation" -> Bottom}]

enter image description here

Using "Orientation" -> Top gives

enter image description here

SeedRandom[1]

TreeGraph[RandomInteger[{1, #}] \[UndirectedEdge] # + 1 & /@ Range[20], 
 ImageSize -> Large, VertexLabels -> Placed["Name", Center], 
 VertexStyle -> White, VertexShapeFunction -> "Square", 
 VertexSize -> Large, EdgeStyle -> Directive[Opacity[1], Gray],
 EdgeShapeFunction -> zigzag["Vertical"]]

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.