5
$\begingroup$

How can I control the label position and formatting using Dendrogram? I am looking for a way which is also easy to use when passing a clustering tree as input (instead of unclustered data).

This example is from the documentation:

Dendrogram[{Entity["City", {"Paris", "IleDeFrance", "France"}], 
  Entity["City", {"Sydney", "NewSouthWales", "Australia"}], 
  Entity["City", {"Boston", "Massachusetts", "UnitedStates"}], 
  Entity["City", {"SanFrancisco", "California", 
    "UnitedStates"}]}, Left]

The formatting is not ideal and definitely not suitable for publication figures:

enter image description here

DendrogramPlot from the HierarchicalClustering package uses left-alignment (or top-alignment) by default, which is better:

DendrogramPlot[{Entity["City", {"Paris", "IleDeFrance", "France"}], 
  Entity["City", {"Sydney", "NewSouthWales", "Australia"}], 
  Entity["City", {"Boston", "Massachusetts", "UnitedStates"}], 
  Entity["City", {"SanFrancisco", "California", "UnitedStates"}]}, 
 DistanceFunction -> QuantityMagnitude@*GeoDistance, 
 LeafLabels -> (# &), Orientation -> Left]

enter image description here

It also has the LeafLabels option, which makes it easier to control labelling independently of what data is passed into the function.

So this is easy to obtain too:

DendrogramPlot[{Entity["City", {"Paris", "IleDeFrance", "France"}], 
  Entity["City", {"Sydney", "NewSouthWales", "Australia"}], 
  Entity["City", {"Boston", "Massachusetts", "UnitedStates"}], 
  Entity["City", {"SanFrancisco", "California", "UnitedStates"}]}, 
 DistanceFunction -> QuantityMagnitude@*GeoDistance, 
 LeafLabels -> (Rotate[#, Pi/2] &)]

enter image description here

What if I'm stuck with Dendrogram and a pre-computed clustering tree?

$\endgroup$

1 Answer 1

5
$\begingroup$

There are several possibilities. The first example is being adversely affected by the length of the labels. This can be fixed in a round-about manner by altering the DistanceFunction, e.g.

entities = {Entity["City", {"Paris", "IleDeFrance", "France"}], 
 Entity["City", {"Sydney", "NewSouthWales", "Australia"}], 
 Entity["City", {"Boston", "Massachusetts", "UnitedStates"}], 
 Entity["City", {"SanFrancisco", "California", "UnitedStates"}]};
Dendrogram[entities, Left, DistanceFunction -> (GeoDistance[##]^(1/3) &)]

enter image description here

which does well even when the image size is very small. Additionally, it is straightforward to generate the rotated text shown in your third example by using the

{data -> label ...}

form for the data, e.g.

data = (# -> Rotate[Style[CommonName@#, FontSize -> 14], Pi/2]) & /@ entities;
Dendrogram[entities]

enter image description here

But, that still makes the lines look ragged. This can be resolved by modifying the underlying graphics object:

Dendrogram[..., Left] /. Inset[g_, p_] :> Inset[g, p, Scaled[{0, 0.5}]]
Dendrogram[..., Right] /. Inset[g_, p_] :> Inset[g, p, Scaled[{1, 0.5}]]
Dendrogram[..., Top|Bottom] /. Inset[g_, p_] :> Inset[g, p, Scaled[{0.5, 1}]]

enter image description here enter image description here

enter image description here enter image description here

$\endgroup$
6
  • $\begingroup$ The Top|Bottom is just for illustration, not real syntax, right? $\endgroup$
    – Szabolcs
    Oct 3, 2016 at 14:32
  • $\begingroup$ @Szabolcs as in either Top (default) or Bottom. I didn't want to write it out twice. :) $\endgroup$
    – rcollyer
    Oct 3, 2016 at 14:33
  • $\begingroup$ If I have a clustering tree, the only way to rotate labels is to change them within the tree, right? Example: tree = ClusteringTree[RandomReal[1, 10]];, then Dendrogram[ SetProperty[tree, VertexLabels -> Normal[Rotate[#, Pi/2] & /@ Association@PropertyValue[tree, VertexLabels]]]]. The tree also has a "LeafLabels" graph property, but I don't know if I can count on it being there in future versions as it is not documented. $\endgroup$
    – Szabolcs
    Oct 3, 2016 at 14:35
  • 1
    $\begingroup$ Actually, I am creating it manually, not with ClusteringTree. What I was originally looking for is a general structured representation of the dendrogram/clustering. I thought Dendrogram didn't have this, but it turns out that it does: it is exactly the clustering tree, with a key property being the VertexWeights. This is all nice, but manipulating this tree is not as easy as manipulating Cluster expressions in the HierarchicalClustering package. $\endgroup$
    – Szabolcs
    Oct 3, 2016 at 14:50
  • 1
    $\begingroup$ I think a much better and clearer separation of presentation and computation is needed. Functions to do the clustering and return a hierarchy. Functions for manipulating this hierarchy. Then functions for plotting it. I'll write to support eventually, but not right now. It takes time to make a good argument. Also, both FindClusters and FindGraphCommunities should really really be able to create these hierarchies (independently of plotting) when the method supports it. (They support many methods, not all hierarchical.) $\endgroup$
    – Szabolcs
    Oct 3, 2016 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.