In versions 10.4+, Dendrogram
produces desired shapes when input a Graph
with VertexWeights
.
In the following, we add vertex 0
to CompleteKaryTree
with n
levels and obtain the required VertexWeights
based on the layer a node occupies. The resulting vertex-weighted graph is used as input to Dendrogram
. The function dendrogramF
below constructs the appropriate graph to pass to Dendrogram
and postprocesses the output to produce the desired results.
The first argument of the function dendrogramF
(nk = {n, k}
) controls the number of layers (n
) and, optionally, the arity (k
). A singleton list {n}
for the argument gives binary tree with n
levels. The second argument (length
) of the function modifies the vertex weights to give the desired length for the vertical sections. The third argument (w
) controls the AbsoluteThickness
of the lines to give the desired channel widths. We don't have an argument to control Total width directly; desired horizontal width can be obtained playing with the settings of the ImageSize
option.
dendrogramF[color_ : Black, o : OptionsPattern[]] :=
Module[{nk = #, length = #2, w = #3, ckat, ckat2, edges, vertices, vertexweights},
ckat = CompleteKaryTree[##& @@ nk, DirectedEdges -> True];
edges = Sort[Sort /@ EdgeList[ckat2 = EdgeAdd[ckat, 0 -> 1]]];
vertices = Sort@VertexList[ckat2];
vertexweights = length (Reverse[ArrayComponents[Reverse[Length /@
(VertexOutComponent[ckat2, #] & /@ vertices)]]] - 1);
Show[Dendrogram[Graph[edges, VertexWeight -> vertexweights], Bottom] /.
l : Line[__] :> {color, AbsoluteThickness[w], (l /. {{a_, b_}, {a_, b_}} :> Nothing)},
o, AspectRatio -> 1/2]] &;
Examples:
dendrogramF[][{3}, 1, 20]
dendrogramF[][{4}, 1, 15]
dendrogramF[Black, AspectRatio -> 1/2, ImageSize -> 600][{4, 3}, 1, 10]
dendrogramF[Black, AspectRatio -> 1/4, ImageSize -> 700][{5, 3}, 1, 5]
dendrogramF[Blue, AspectRatio -> 1/4, ImageSize -> 500][{3, 6}, 1, 5]
dendrogramF[Blue, AspectRatio -> 1/4, ImageSize -> 700][{6, 3}, 1, 1]
dendrogramF[Red, Frame -> True, AspectRatio -> 1, ImageSize -> 400,
PlotRangePadding -> {{10, 10}, {1, 1}}][{5}, 1, 10]
dendrogramF[Red, Frame->True, AspectRatio -> 1, ImageSize -> 400,
PlotRangePadding->{{10, 10},{5, 5}}][{5}, 10, 10]