7
$\begingroup$

Is there any way to have each cluster in the DendrogramPlot highlight in a different color? I've tried the obvious way but it doesn't work:

Needs["HierarchicalClustering`"]
data = {{0.758587,0.844749},{1.05549,1.01289},{1.21689,1.1919},{0.53086,0.977943},{2.08582,1.8978},{2.10121,1.81776},{1.88505,1.83257},{1.89067,2.48317},{2.88711,1.02682},{2.82199,0.901863},{2.90456,0.91189},{2.80032,1.09192}};
DendrogramPlot[
 data,
 LeafLabels -> Range[12],
 HighlightLevel -> 3,
 HighlightStyle -> {Red, Green, Blue}
 ]

Mathematica graphics

$\endgroup$
7
$\begingroup$

If you want something like this :

enter image description here

(colors are random) the code is :

dendogram = 
   DendrogramPlot[data, LeafLabels -> Range[12], HighlightLevel -> 3, 
    HighlightStyle -> {Red, Green, Blue}];

 Show[
  dendogram,
  Graphics[(Cases[dendogram, Rectangle[___], {1, Infinity}] // 
      SortBy[#, -#[[2, 2]] &] & ) /. 
    x : Rectangle[___] :> {Hue[RandomReal[]], x}],
  Graphics[Cases[dendogram, Line[___], {1, Infinity}]]
  ]

or like this :

enter image description here

(agglomerate colors, colors not random) then :

Needs["HierarchicalClustering`"]
data = {{0.758587, 0.844749}, {1.05549, 1.01289}, {1.21689, 
    1.1919}, {0.53086, 0.977943}, {2.08582, 1.8978}, {2.10121, 
    1.81776}, {1.88505, 1.83257}, {1.89067, 2.48317}, {2.88711, 
    1.02682}, {2.82199, 0.901863}, {2.90456, 0.91189}, {2.80032, 
    1.09192}};

dendogram = 
  DendrogramPlot[data, LeafLabels -> Range[12], HighlightLevel -> 3, 
   HighlightStyle -> {Red, Green, Blue}];

intervalList = Cases[dendogram, Rectangle[___], {1, Infinity}] //
   (*Take[#,3]& //*)
   (Interval[{#[[1, 1]], #[[2, 1]]}] & /@ # &) //
  FixedPoint[
    Replace[#, {a0___, i1_, a1___, i2_, a3___} /; 
        IntervalIntersection[i1, i2] =!= Interval[] :> {a0, 
        IntervalUnion[i1, i2], a1, a3}] &, #] &

ClearAll[color];
color[1] = Red; color[2] = Green;
color[3] = Blue; color[_] = Hue[RandomReal[]];

Show[
 dendogram,
 Graphics[
  Cases[dendogram, Rectangle[___], {1, Infinity}] /. 
   x : Rectangle[___] :> {With[{pos = x[[1, 1]]}, 
      color[
       Position[intervalList, _?(IntervalMemberQ[#, pos] &)][[1, 
         1]]]], x}], 
 Graphics[Cases[dendogram, Line[___], {1, Infinity}]]
 ]
$\endgroup$
  • $\begingroup$ I was hoping for that each cluster, according to the HighlightLevel would have each own color. In the example the cluster of 1, 4, 2, 3 would have one color and the cluster of 7, 5, 6, 8 would have another color and so on. $\endgroup$ – Mr Alpha Feb 10 '13 at 14:30
2
$\begingroup$
 Needs["HierarchicalClustering`"] 

 ClearAll[highlightLevels];
 highlightLevels[data_, level_Integer: 2, colors_List: ColorData[1, "ColorList"], opts : OptionsPattern[]] :=
 Module[{dndrgm =  DendrogramPlot[data, LeafLabels -> Automatic, HighlightLevel -> level],
 rectCoords, lines, labels, splts, newCoords, newRectangles,  colorlist},
 If[level == 1,
  DendrogramPlot[data, LeafLabels -> Automatic, HighlightLevel -> 1, 
  HighlightStyle -> colors[[1]], opts],
  rectCoords = Cases[dndrgm, Rectangle[x__] :> {x}, {0, Infinity}];
  lines = Cases[dndrgm, Line[__], {0, Infinity}];
  labels = Cases[dndrgm, Text[__], {0, Infinity}];
  splts = Split[SortBy[ rectCoords, #[[1, 1]] &], (#1[[1, 1]] <= #2[[1, 1]] <= #1[[2, 1]] &)];
 newCoords = Join @@@ Gather[splts, #2[[1, 1, 1]] <= Max@#1[[;; , 2, 1]] &];
 newRectangles = Rectangle @@@ # & /@ newCoords;
 colorlist = colors[[Mod[#, Length[colors], 1]]] & /@  Range[Length[newRectangles]]; 
 Graphics[
Join[Riffle[colorlist, newRectangles], {Black}, lines, labels], PlotRange -> All, opts]]]

Example:

data = {{0.758587, 0.844749}, {1.05549, 1.01289}, {1.21689, 1.1919},
 {0.53086, 0.977943}, {2.08582, 1.8978}, {2.10121, 1.81776}, 
 {1.88505, 1.83257}, {1.89067, 2.48317}, {2.88711, 1.02682},
 {2.82199, 0.901863}, {2.90456, 0.91189}, {2.80032, 1.09192}};
Grid[Partition[highlightLevels[data, #, ColorData[1, "ColorList"][[2 ;;]],
 AspectRatio -> 1, ImageSize -> 250] & /@ Range[9], 3],  Dividers -> All]

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.