821 lines
666 KiB
HTML
Raw Permalink Normal View History

2025-01-12 00:52:51 +08:00
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="author" content="Tal Galili" />
<meta name="date" content="2024-11-15" />
<title>Frequently asked questions</title>
<script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to
// be compatible with the behavior of Pandoc < 2.8).
document.addEventListener('DOMContentLoaded', function(e) {
var hs = document.querySelectorAll("div.section[class*='level'] > :first-child");
var i, h, a;
for (i = 0; i < hs.length; i++) {
h = hs[i];
if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6
a = h.attributes;
while (a.length > 0) h.removeAttribute(a[0].name);
}
});
</script>
<style type="text/css">
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
</style>
<style type="text/css">
code {
white-space: pre;
}
.sourceCode {
overflow: visible;
}
</style>
<style type="text/css" data-origin="pandoc">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { display: inline-block; text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; }
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; }
code span.at { color: #7d9029; }
code span.bn { color: #40a070; }
code span.bu { color: #008000; }
code span.cf { color: #007020; font-weight: bold; }
code span.ch { color: #4070a0; }
code span.cn { color: #880000; }
code span.co { color: #60a0b0; font-style: italic; }
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; }
code span.do { color: #ba2121; font-style: italic; }
code span.dt { color: #902000; }
code span.dv { color: #40a070; }
code span.er { color: #ff0000; font-weight: bold; }
code span.ex { }
code span.fl { color: #40a070; }
code span.fu { color: #06287e; }
code span.im { color: #008000; font-weight: bold; }
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; }
code span.kw { color: #007020; font-weight: bold; }
code span.op { color: #666666; }
code span.ot { color: #007020; }
code span.pp { color: #bc7a00; }
code span.sc { color: #4070a0; }
code span.ss { color: #bb6688; }
code span.st { color: #4070a0; }
code span.va { color: #19177c; }
code span.vs { color: #4070a0; }
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; }
</style>
<script>
// apply pandoc div.sourceCode style to pre.sourceCode instead
(function() {
var sheets = document.styleSheets;
for (var i = 0; i < sheets.length; i++) {
if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue;
try { var rules = sheets[i].cssRules; } catch (e) { continue; }
var j = 0;
while (j < rules.length) {
var rule = rules[j];
// check if there is a div.sourceCode rule
if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") {
j++;
continue;
}
var style = rule.style.cssText;
// check if color or background-color is set
if (rule.style.color === '' && rule.style.backgroundColor === '') {
j++;
continue;
}
// replace div.sourceCode by a pre.sourceCode rule
sheets[i].deleteRule(j);
sheets[i].insertRule('pre.sourceCode{' + style + '}', j);
}
}
})();
</script>
<style type="text/css">body {
background-color: #fff;
margin: 1em auto;
max-width: 700px;
overflow: visible;
padding-left: 2em;
padding-right: 2em;
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
line-height: 1.35;
}
#TOC {
clear: both;
margin: 0 0 10px 10px;
padding: 4px;
width: 400px;
border: 1px solid #CCCCCC;
border-radius: 5px;
background-color: #f6f6f6;
font-size: 13px;
line-height: 1.3;
}
#TOC .toctitle {
font-weight: bold;
font-size: 15px;
margin-left: 5px;
}
#TOC ul {
padding-left: 40px;
margin-left: -1.5em;
margin-top: 5px;
margin-bottom: 5px;
}
#TOC ul ul {
margin-left: -2em;
}
#TOC li {
line-height: 16px;
}
table {
margin: 1em auto;
border-width: 1px;
border-color: #DDDDDD;
border-style: outset;
border-collapse: collapse;
}
table th {
border-width: 2px;
padding: 5px;
border-style: inset;
}
table td {
border-width: 1px;
border-style: inset;
line-height: 18px;
padding: 5px 5px;
}
table, table th, table td {
border-left-style: none;
border-right-style: none;
}
table thead, table tr.even {
background-color: #f7f7f7;
}
p {
margin: 0.5em 0;
}
blockquote {
background-color: #f6f6f6;
padding: 0.25em 0.75em;
}
hr {
border-style: solid;
border: none;
border-top: 1px solid #777;
margin: 28px 0;
}
dl {
margin-left: 0;
}
dl dd {
margin-bottom: 13px;
margin-left: 13px;
}
dl dt {
font-weight: bold;
}
ul {
margin-top: 0;
}
ul li {
list-style: circle outside;
}
ul ul {
margin-bottom: 0;
}
pre, code {
background-color: #f7f7f7;
border-radius: 3px;
color: #333;
white-space: pre-wrap;
}
pre {
border-radius: 3px;
margin: 5px 0px 10px 0px;
padding: 10px;
}
pre:not([class]) {
background-color: #f7f7f7;
}
code {
font-family: Consolas, Monaco, 'Courier New', monospace;
font-size: 85%;
}
p > code, li > code {
padding: 2px 0px;
}
div.figure {
text-align: center;
}
img {
background-color: #FFFFFF;
padding: 2px;
border: 1px solid #DDDDDD;
border-radius: 3px;
border: 1px solid #CCCCCC;
margin: 0 5px;
}
h1 {
margin-top: 0;
font-size: 35px;
line-height: 40px;
}
h2 {
border-bottom: 4px solid #f7f7f7;
padding-top: 10px;
padding-bottom: 2px;
font-size: 145%;
}
h3 {
border-bottom: 2px solid #f7f7f7;
padding-top: 10px;
font-size: 120%;
}
h4 {
border-bottom: 1px solid #f7f7f7;
margin-left: 8px;
font-size: 105%;
}
h5, h6 {
border-bottom: 1px solid #ccc;
font-size: 105%;
}
a {
color: #0033dd;
text-decoration: none;
}
a:hover {
color: #6666ff; }
a:visited {
color: #800080; }
a:visited:hover {
color: #BB00BB; }
a[href^="http:"] {
text-decoration: underline; }
a[href^="https:"] {
text-decoration: underline; }
code > span.kw { color: #555; font-weight: bold; }
code > span.dt { color: #902000; }
code > span.dv { color: #40a070; }
code > span.bn { color: #d14; }
code > span.fl { color: #d14; }
code > span.ch { color: #d14; }
code > span.st { color: #d14; }
code > span.co { color: #888888; font-style: italic; }
code > span.ot { color: #007020; }
code > span.al { color: #ff0000; font-weight: bold; }
code > span.fu { color: #900; font-weight: bold; }
code > span.er { color: #a61717; background-color: #e3d2d2; }
</style>
</head>
<body>
<h1 class="title toc-ignore">Frequently asked questions</h1>
<h4 class="author">Tal Galili</h4>
<h4 class="date">2024-11-15</h4>
<div id="TOC">
<ul>
<li><a href="#introduction" id="toc-introduction">Introduction</a>
<ul>
<li><a href="#how-to-colour-the-labels-of-a-dendrogram-by-an-additional-factor-variable" id="toc-how-to-colour-the-labels-of-a-dendrogram-by-an-additional-factor-variable">How
to colour the labels of a dendrogram by an additional factor
variable</a></li>
<li><a href="#how-to-color-a-dendrograms-labels-according-to-defined-groups-in-r" id="toc-how-to-color-a-dendrograms-labels-according-to-defined-groups-in-r">How
to color a dendrograms labels according to defined groups? (in
R)</a></li>
<li><a href="#how-to-color-a-dendrograms-brancheslabels-based-on-cluster-i.e.-cutree-result" id="toc-how-to-color-a-dendrograms-brancheslabels-based-on-cluster-i.e.-cutree-result">How
to color a dendrograms branches/labels based on cluster (i.e.: cutree
result)</a></li>
<li><a href="#change-dendrograms-labels" id="toc-change-dendrograms-labels">Change dendrograms labels</a></li>
<li><a href="#larger-font-for-leaves-in-a-dendrogram" id="toc-larger-font-for-leaves-in-a-dendrogram">Larger font for leaves
in a dendrogram</a></li>
<li><a href="#how-to-view-attributes-of-a-dendrogram" id="toc-how-to-view-attributes-of-a-dendrogram">How to view attributes
of a dendrogram</a></li>
<li><a href="#how-to-color-the-branches-in-heatmap.2" id="toc-how-to-color-the-branches-in-heatmap.2">How to color the
branches in heatmap.2?</a></li>
<li><a href="#for-package-developers---how-to-call-imported-calls-from-dendextend-0.18.3" id="toc-for-package-developers---how-to-call-imported-calls-from-dendextend-0.18.3">For
package developers - how to call imported calls from dendextend
0.18.3?</a></li>
<li><a href="#how-to-plot-a-fan-polar-dendrogram-in-r" id="toc-how-to-plot-a-fan-polar-dendrogram-in-r">How to plot a fan
(Polar) Dendrogram in R?</a></li>
<li><a href="#a-way-to-calculate-lowest-value-of-h-in-cut-that-produces-groupings-of-a-given-minimum-size" id="toc-a-way-to-calculate-lowest-value-of-h-in-cut-that-produces-groupings-of-a-given-minimum-size">A
way to calculate lowest value of h in cut that produces groupings of a
given minimum size?</a></li>
<li><a href="#coloring-dendrograms-end-branches-or-leaves-based-on-column-number-of-data-frame-in-r" id="toc-coloring-dendrograms-end-branches-or-leaves-based-on-column-number-of-data-frame-in-r">Coloring
dendrograms end branches (or leaves) based on column number of data
frame in R</a></li>
<li><a href="#color-side-bar-dendrogram-plot" id="toc-color-side-bar-dendrogram-plot">Color side bar dendrogram
plot</a></li>
<li><a href="#plot-a-mirror-labels-on-the-left-horizontal-dendrogram" id="toc-plot-a-mirror-labels-on-the-left-horizontal-dendrogram">Plot a
“mirror” (labels on the left) horizontal dendrogram</a></li>
</ul></li>
</ul>
</div>
<!--
%\VignetteEngine{knitr::rmarkdown}
%\VignetteIndexEntry{Frequently asked questions}
-->
<div id="introduction" class="section level2">
<h2>Introduction</h2>
<p>Questions are often taken from here the <a href="https://stackoverflow.com/questions/tagged/dendrogram+r">stackoverflow
<code>dendrogram</code> tag</a>.</p>
<div id="how-to-colour-the-labels-of-a-dendrogram-by-an-additional-factor-variable" class="section level3">
<h3>How to colour the labels of a dendrogram by an additional factor
variable</h3>
<p>Asked (<a href="https://stackoverflow.com/questions/27485549/how-to-colour-the-labels-of-a-dendrogram-by-an-additional-factor-variable-in-r" class="uri">https://stackoverflow.com/questions/27485549/how-to-colour-the-labels-of-a-dendrogram-by-an-additional-factor-variable-in-r</a>)[here].</p>
<p>Solution: use the <code>labels_colors</code> function.</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" tabindex="-1"></a><span class="co"># install.packages(&quot;dendextend&quot;)</span></span>
<span id="cb1-2"><a href="#cb1-2" tabindex="-1"></a><span class="fu">library</span>(dendextend)</span>
<span id="cb1-3"><a href="#cb1-3" tabindex="-1"></a></span>
<span id="cb1-4"><a href="#cb1-4" tabindex="-1"></a>dend <span class="ot">&lt;-</span> <span class="fu">as.dendrogram</span>(<span class="fu">hclust</span>(<span class="fu">dist</span>(USArrests[<span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>,])))</span>
<span id="cb1-5"><a href="#cb1-5" tabindex="-1"></a><span class="co"># Like: </span></span>
<span id="cb1-6"><a href="#cb1-6" tabindex="-1"></a><span class="co"># dend &lt;- USArrests[1:5,] %&gt;% dist %&gt;% hclust %&gt;% as.dendrogram</span></span>
<span id="cb1-7"><a href="#cb1-7" tabindex="-1"></a></span>
<span id="cb1-8"><a href="#cb1-8" tabindex="-1"></a><span class="co"># By default, the dend has no colors to the labels</span></span>
<span id="cb1-9"><a href="#cb1-9" tabindex="-1"></a><span class="fu">labels_colors</span>(dend)</span></code></pre></div>
<pre><code>## NULL</code></pre>
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" tabindex="-1"></a><span class="fu">par</span>(<span class="at">mfrow =</span> <span class="fu">c</span>(<span class="dv">1</span>,<span class="dv">2</span>))</span>
<span id="cb3-2"><a href="#cb3-2" tabindex="-1"></a><span class="fu">plot</span>(dend, <span class="at">main =</span> <span class="st">&quot;Original dend&quot;</span>)</span>
<span id="cb3-3"><a href="#cb3-3" tabindex="-1"></a></span>
<span id="cb3-4"><a href="#cb3-4" tabindex="-1"></a><span class="co"># let&#39;s add some color:</span></span>
<span id="cb3-5"><a href="#cb3-5" tabindex="-1"></a><span class="fu">labels_colors</span>(dend) <span class="ot">&lt;-</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">5</span></span>
<span id="cb3-6"><a href="#cb3-6" tabindex="-1"></a><span class="co"># Now each state has a color</span></span>
<span id="cb3-7"><a href="#cb3-7" tabindex="-1"></a><span class="fu">labels_colors</span>(dend) </span></code></pre></div>
<pre><code>## Arkansas Arizona California Alabama Alaska
## 1 2 3 4 5</code></pre>
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" tabindex="-1"></a><span class="fu">plot</span>(dend, <span class="at">main =</span> <span class="st">&quot;A color for every state&quot;</span>)</span></code></pre></div>
<p><img role="img" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAlgAAADhCAYAAAAUPMtIAAAEDmlDQ1BrQ0dDb2xvclNwYWNlR2VuZXJpY1JHQgAAOI2NVV1oHFUUPpu5syskzoPUpqaSDv41lLRsUtGE2uj+ZbNt3CyTbLRBkMns3Z1pJjPj/KRpKT4UQRDBqOCT4P9bwSchaqvtiy2itFCiBIMo+ND6R6HSFwnruTOzu5O4a73L3PnmnO9+595z7t4LkLgsW5beJQIsGq4t5dPis8fmxMQ6dMF90A190C0rjpUqlSYBG+PCv9rt7yDG3tf2t/f/Z+uuUEcBiN2F2Kw4yiLiZQD+FcWyXYAEQfvICddi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V7/+/16ffMD1N8AuKD/A/8leAvFY9bLAAAAOGVYSWZNTQAqAAAACAABh2kABAAAAAEAAAAaAAAAAAACoAIABAAAAAEAAAJYoAMABAAAAAEAAADhAAAAANxLa8wAAEAASURBVHgB7Z0HfBTFF8cfJKQQCCGhJPSOdFCqf0BRwAYKFgSxgAUUsSEiYkexKyIoWFBREVFpIlJEpYmKgtIRKdJCCakkkH7/+Q1uuISUS7jb3dz9Hp/jbndnp3zn7uXtzJs3ZRxKhEICJEACJEACJEACJOA2AmXdlhMzIgESIAESIAESIAES0ARoYPGLQAIkQAIkQAIkQAJuJkADy81AmR0JkAAJkAAJkAAJ0MDid4AESIAESIAESIAE3EyABpabgTI7EiABEiABEiABEqCBxe8ACZAACZAACZAACbiZAA0sNwNldiRAAiRAAiRAAiRAA4vfARIgARIgARIgARJwMwEaWG4GyuxIgARIgARIgARIgAYWvwMkQAIkQAIkQAIk4GYCNLDcDJTZkQAJkAAJkAAJkAANLH4HSIAESIAESIAESMDNBGhguRkosyMBEiABEiABEiABGlj8DpAACZAACZAACZCAmwnQwHIzUGZHAiRAAiRAAiRAAjSw+B0gARIgARIgARIgATcToIHlZqDMjgRIgARIgARIgARoYPE7QAIkQAIkQAIkQAJuJkADy81AmR0JkAAJkAAJkAAJ0MDid4AESIAESIAESIAE3EyABpabgTI7EiABEiABEiABEqCBxe8ACZAACZAACZAACbiZAA0sNwNldiRAAiRAAiRAAiRAA4vfARIgARIgARIgARJwMwEaWG4GasfskpKS5ODBg3asGutEAiRAAqYRSEhIkEOHDplWHgvybQI0sLy0/zMzM+WZZ56R+vXrS1hYmNSuXVsqVaokN954oxw5csSlVlepUkXKlCkje/bscSk9Et1zzz36npdfftnle1xJWJK6PPnkk7oujzzyiCtFnFOa8847T5f1+++/n1M+vJkErCYwe/Zs/V3Gb//LL780rTol+Y27WrmMjAzp16+fhIeHS61atWTAgAGu3uqz6TZv3iyff/55sdqPh/kXX3yxWPd4c2IaWF7au1Agzz77rPz777/ayOrYsa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rYwUR2RkpKN8+fL63JgxY3Sau+++Wx+rH74+Vj9kfYx8kV4pYYfyCdPnRo4cqdP8/fffuk5IM3DgQEebNm309QYNGujr+C9vXXIu/Pfh448/1vegbuoPgy5LTW3qc6NHj9ap9u/f71BTBPqcMp4cyB/plfLLyQ51UE/uDmVAOq6++mqH+kOg0yjl7lB+bTqdMkD1OeSFz+XKlXMYZa1bty4nL34ggdJGoFGjRvq7rQwBh/F7Vz5LOd/9/NqTnJzsQBr8lurVq+dQf4D1ZzVd5Dh16pS+RU0b6XNIA/2kXA/0sXqQysky729cGUr6d4h7lNHmqFatmr4H+kONSOn7rr32Wn0uODjYof7oa92ijJWcPPHhvffe0/VCPqGhoQ419eVQBpbDlfyV4ZaTP+oMf
<p>Instead of using <code>1:5</code>, we can obviously use colors that
are based on another factor (organized): the labels themselves. But in
such a case, we want to map between the order of the labels and the
order of the items in the original dataset. Here is another example
based on the iris dataset:</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" tabindex="-1"></a><span class="co"># install.packages(&quot;dendextend&quot;)</span></span>
<span id="cb6-2"><a href="#cb6-2" tabindex="-1"></a><span class="fu">library</span>(dendextend)</span>
<span id="cb6-3"><a href="#cb6-3" tabindex="-1"></a></span>
<span id="cb6-4"><a href="#cb6-4" tabindex="-1"></a>small_iris <span class="ot">&lt;-</span> iris[<span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">51</span>, <span class="dv">101</span>, <span class="dv">2</span>, <span class="dv">52</span>, <span class="dv">102</span>), ]</span>
<span id="cb6-5"><a href="#cb6-5" tabindex="-1"></a>dend <span class="ot">&lt;-</span> <span class="fu">as.dendrogram</span>(<span class="fu">hclust</span>(<span class="fu">dist</span>(small_iris[,<span class="sc">-</span><span class="dv">5</span>])))</span>
<span id="cb6-6"><a href="#cb6-6" tabindex="-1"></a><span class="co"># Like: </span></span>
<span id="cb6-7"><a href="#cb6-7" tabindex="-1"></a><span class="co"># dend &lt;- small_iris[,-5] %&gt;% dist %&gt;% hclust %&gt;% as.dendrogram</span></span>
<span id="cb6-8"><a href="#cb6-8" tabindex="-1"></a></span>
<span id="cb6-9"><a href="#cb6-9" tabindex="-1"></a><span class="co"># By default, the dend has no colors to the labels</span></span>
<span id="cb6-10"><a href="#cb6-10" tabindex="-1"></a><span class="fu">labels_colors</span>(dend)</span></code></pre></div>
<pre><code>## NULL</code></pre>
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" tabindex="-1"></a><span class="fu">par</span>(<span class="at">mfrow =</span> <span class="fu">c</span>(<span class="dv">1</span>,<span class="dv">2</span>))</span>
<span id="cb8-2"><a href="#cb8-2" tabindex="-1"></a><span class="fu">plot</span>(dend, <span class="at">main =</span> <span class="st">&quot;Original dend&quot;</span>)</span>
<span id="cb8-3"><a href="#cb8-3" tabindex="-1"></a></span>
<span id="cb8-4"><a href="#cb8-4" tabindex="-1"></a><span class="co"># Let&#39;s add some color:</span></span>
<span id="cb8-5"><a href="#cb8-5" tabindex="-1"></a>colors_to_use <span class="ot">&lt;-</span> <span class="fu">as.numeric</span>(small_iris[,<span class="dv">5</span>])</span>
<span id="cb8-6"><a href="#cb8-6" tabindex="-1"></a>colors_to_use</span></code></pre></div>
<pre><code>## [1] 1 2 3 1 2 3</code></pre>
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" tabindex="-1"></a><span class="co"># But sort them based on their order in dend:</span></span>
<span id="cb10-2"><a href="#cb10-2" tabindex="-1"></a>colors_to_use <span class="ot">&lt;-</span> colors_to_use[<span class="fu">order.dendrogram</span>(dend)]</span>
<span id="cb10-3"><a href="#cb10-3" tabindex="-1"></a>colors_to_use</span></code></pre></div>
<pre><code>## [1] 1 1 2 2 3 3</code></pre>
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" tabindex="-1"></a><span class="co"># Now we can use them</span></span>
<span id="cb12-2"><a href="#cb12-2" tabindex="-1"></a><span class="fu">labels_colors</span>(dend) <span class="ot">&lt;-</span> colors_to_use</span>
<span id="cb12-3"><a href="#cb12-3" tabindex="-1"></a><span class="co"># Now each state has a color</span></span>
<span id="cb12-4"><a href="#cb12-4" tabindex="-1"></a><span class="fu">labels_colors</span>(dend) </span></code></pre></div>
<pre><code>## 1 2 51 52 101 102
## 1 1 2 2 3 3</code></pre>
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" tabindex="-1"></a><span class="fu">plot</span>(dend, <span class="at">main =</span> <span class="st">&quot;A color for every Species&quot;</span>)</span></code></pre></div>
<p><img role="img" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAlgAAADhCAYAAAAUPMtIAAAEDmlDQ1BrQ0dDb2xvclNwYWNlR2VuZXJpY1JHQgAAOI2NVV1oHFUUPpu5syskzoPUpqaSDv41lLRsUtGE2uj+ZbNt3CyTbLRBkMns3Z1pJjPj/KRpKT4UQRDBqOCT4P9bwSchaqvtiy2itFCiBIMo+ND6R6HSFwnruTOzu5O4a73L3PnmnO9+595z7t4LkLgsW5beJQIsGq4t5dPis8fmxMQ6dMF90A190C0rjpUqlSYBG+PCv9rt7yDG3tf2t/f/Z+uuUEcBiN2F2Kw4yiLiZQD+FcWyXYAEQfvICddi+AnEO2ycIOISw7UAVxieD/Cyz5mRMohfRSwoqoz+xNuIB+cj9loEB3Pw2448NaitKSLLRck2q5pOI9O9g/t/tkXda8Tbg0+PszB9FN8DuPaXKnKW4YcQn1Xk3HSIry5ps8UQ/2W5aQnxIwBdu7yFcgrxPsRjVXu8HOh0qao30cArp9SZZxDfg3h1wTzKxu5E/LUxX5wKdX5SnAzmDx4A4OIqLbB69yMesE1pKojLjVdoNsfyiPi45hZmAn3uLWdpOtfQOaVmikEs7ovj8hFWpz7EV6mel0L9Xy23FMYlPYZenAx0yD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yRB98ZsyYYR+8Up2JOgxu6MP9998vQ4cOTfUmVUr+N954w56vtn3ZsmUya9YsmT59ut33ySef2N+BSlVQzpMr8vtRzip8nx0Hy/cqKr+AOhygSbuG9cnOTeooPfHEE3ZTu5Jzc3OlqKhITjzxRPnZz34m2t3eqFEjOf/8820efQLSY64R051PPvmktGvXTrT79+qrr5aXXnrJ5nnkkUfsOX/729/s9j/+8Q+7/emnn9ptvemffvpp23tWv359+0S1bds2m0f/mzx5slx88cW23JYtW8p5551njUQoQwxftG0qW4MGDeQ3v/mNaBxCybRw4ULbVh0u1by33357WJzCGWecISeddJIsWLBAzjrrLNvVfsopp8jHH38cKiovL8+2XVm1bt3aMvX66TAkDF8gEIXAhAkT7H1+0UUXydlnny16rbr3Z1mn6n2vPeFHHHGEHXa69tpr5Ztvvgk75YUXXpBevXrZMITjjjtO7rzzTtm/f39YnpIbem9dfvnl0rx5c2nWrJm1U8V71N9++21rM9SW/OpXv7I2aezYsWHFaMyV2ia1X5p++tOfyr333mu/5+fnyx/+8Afp3LmzvX9PP/100YdON8VSvpu3LHtxzTXXWBlef/11N7ts3rzZ7jvttNNk3759dv/48ePl+OOPt0OYOpLw5ptvhvKXJos6i506dbLlqI12k9pebfPIkSPdXWF/9+zZY7fnz59v5dAN1c0///lPa181VETTbbfdZstR50tDRtRennDCCTJlyhR73P2vLLk1T7Tro7Tfj2hlaliLDvfq0GaTJk3ksssuK9WOuzL6/q+52UgBI2BuTu0Ldozzc0jLjEFyzMVrj8+ePdspLCy0383TrWOG9xzjeDhXXHGFPc/cePaY6QGz28aY2m0t2zhkTtOmTZ1atWrZfX/84x9tnuuuu85uGyNht9999127reVqfmOwHXOj233XX3+9zfPtt99amTTPpZde6vzkJz+xx82NZo/rfyVlCR3435fnn3/enqOymR8RW5cZ2rT7jLG1uUzXuVO7dm27zzhPjpav+Y2hDBWnMpinfMc4kE7v3r0d86Nh85gfAsfEtdl8xgG1+7Qs/V6jRg3HrWvevHmhsvgCAT8QaNOmjb1ejSPguPewcW5C13NpMu7evdvRPHp/tGrVyjEPHva76QF3jANjT3nsscfsPs2jNscMCdlt83AUKrKkPVi0aJG9t/QcE
</div>
<div id="how-to-color-a-dendrograms-labels-according-to-defined-groups-in-r" class="section level3">
<h3>How to color a dendrograms labels according to defined groups? (in
R)</h3>
<p>Asked (<a href="https://stackoverflow.com/questions/31117849/how-to-color-a-dendrograms-labels-according-to-defined-groups-in-r" class="uri">https://stackoverflow.com/questions/31117849/how-to-color-a-dendrograms-labels-according-to-defined-groups-in-r</a>)[here].</p>
<p>Solution: use the <code>color_labels</code> function.</p>
<p>I suspect the function you are looking for is either
<code>color_labels</code> or <code>get_leaves_branches_col</code>. The
first color your labels based on <code>cutree</code> (like
<code>color_branches</code> do) and the second allows you to get the
colors of the branch of each leaf, and then use it to color the labels
of the tree (if you use unusual methods for coloring the branches (as
happens when using <code>branches_attr_by_labels</code>). For
example:</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" tabindex="-1"></a> <span class="co"># define dendrogram object to play with:</span></span>
<span id="cb15-2"><a href="#cb15-2" tabindex="-1"></a> hc <span class="ot">&lt;-</span> <span class="fu">hclust</span>(<span class="fu">dist</span>(USArrests[<span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>,]), <span class="st">&quot;ave&quot;</span>)</span>
<span id="cb15-3"><a href="#cb15-3" tabindex="-1"></a> dend <span class="ot">&lt;-</span> <span class="fu">as.dendrogram</span>(hc)</span>
<span id="cb15-4"><a href="#cb15-4" tabindex="-1"></a></span>
<span id="cb15-5"><a href="#cb15-5" tabindex="-1"></a> <span class="fu">library</span>(dendextend)</span>
<span id="cb15-6"><a href="#cb15-6" tabindex="-1"></a> <span class="fu">par</span>(<span class="at">mfrow =</span> <span class="fu">c</span>(<span class="dv">1</span>,<span class="dv">2</span>), <span class="at">mar =</span> <span class="fu">c</span>(<span class="dv">5</span>,<span class="dv">2</span>,<span class="dv">1</span>,<span class="dv">0</span>))</span>
<span id="cb15-7"><a href="#cb15-7" tabindex="-1"></a> dend <span class="ot">&lt;-</span> dend <span class="sc">%&gt;%</span></span>
<span id="cb15-8"><a href="#cb15-8" tabindex="-1"></a> <span class="fu">color_branches</span>(<span class="at">k =</span> <span class="dv">3</span>) <span class="sc">%&gt;%</span></span>
<span id="cb15-9"><a href="#cb15-9" tabindex="-1"></a> <span class="fu">set</span>(<span class="st">&quot;branches_lwd&quot;</span>, <span class="fu">c</span>(<span class="dv">2</span>,<span class="dv">1</span>,<span class="dv">2</span>)) <span class="sc">%&gt;%</span></span>
<span id="cb15-10"><a href="#cb15-10" tabindex="-1"></a> <span class="fu">set</span>(<span class="st">&quot;branches_lty&quot;</span>, <span class="fu">c</span>(<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">1</span>))</span>
<span id="cb15-11"><a href="#cb15-11" tabindex="-1"></a> </span>
<span id="cb15-12"><a href="#cb15-12" tabindex="-1"></a> <span class="fu">plot</span>(dend)</span>
<span id="cb15-13"><a href="#cb15-13" tabindex="-1"></a></span>
<span id="cb15-14"><a href="#cb15-14" tabindex="-1"></a> dend <span class="ot">&lt;-</span> <span class="fu">color_labels</span>(dend, <span class="at">k =</span> <span class="dv">3</span>)</span>
<span id="cb15-15"><a href="#cb15-15" tabindex="-1"></a> <span class="co"># The same as:</span></span>
<span id="cb15-16"><a href="#cb15-16" tabindex="-1"></a> <span class="co"># labels_colors(dend) &lt;- get_leaves_branches_col(dend)</span></span>
<span id="cb15-17"><a href="#cb15-17" tabindex="-1"></a> <span class="fu">plot</span>(dend)</span></code></pre></div>
<p><img role="img" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcIAAAHCCAYAAAB8GMlFAAAEDmlDQ1BrQ0dDb2xvclNwYWNlR2VuZXJpY1JHQgAAOI2NVV1oHFUUPpu5syskzoPUpqaSDv41lLRsUtGE2uj+ZbNt3CyTbLRBkMns3Z1pJjPj/KRpKT4UQRDBqOCT4P9bwSchaqvt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2j37t0UFhZGd911lzo5hh0jJ+4qzc3NVX943BAJBEBAewKwQe2ZIkcQaIiAR4uQp26/9957dPjwYZo/fz5t2bKFTp48qebBXTbsAOEEG0KK6yDgOwHYoO/s8CQI+ErAwxE+9NBDaiuwffv26kzR559/nviYJ7XweCFHvUACARDQjwBsUD+2yBkE6iLg0TXau3dvdfkErx1s1qyZ+kyvXr3Uc7zp6qBBg+rKx3Ln7boXmuVAQiBDCcAGDcWNwkBAJeDhCN1M3E6Qvw8YMED9cV/DJwiAgP4EYIP6M0YJIOAm4NE16j6JTxAAARAAARBwCgE4QqdoGvUEARAAARColYC0jjAsIZ4SJ42stdI4CQIgoD8B2KD+jFGCNgSkdYSBrhAKjY3ShhJyAQEQaDQB2GCjkeEBkwhI6whN4oliQQAEQAAEbEYAjtBmCoO4IAACIAAC2hKQ1hEW8X6Ei1ZoSwu5gQAIeE0ANug1KtxoMgFpHWE571C/M9VkvCgeBJxLADboXN3brebSOkK7KQLyggAIgAAImEMAjtAc7igVBEAABEDAIgSkdYQhUS0oZkhfi2CGGCDgPAKwQefp3K41ltYRuqIjKCapn131ArlBwPYEYIO2V
<p>Either way, you should always have a look at the <code>set</code>
function, for ideas on what can be done to your dendrogram (this saves
the hassle of remembering all the different functions names).</p>
</div>
<div id="how-to-color-a-dendrograms-brancheslabels-based-on-cluster-i.e.-cutree-result" class="section level3">
<h3>How to color a dendrograms branches/labels based on cluster (i.e.:
cutree result)</h3>
<p>Use the <code>color_branches</code> and <code>color_labels</code>
functions, with the <code>k</code> (or<code>h</code>) parameter:</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" tabindex="-1"></a><span class="co"># install.packages(&quot;dendextend&quot;)</span></span>
<span id="cb16-2"><a href="#cb16-2" tabindex="-1"></a><span class="fu">library</span>(dendextend)</span>
<span id="cb16-3"><a href="#cb16-3" tabindex="-1"></a></span>
<span id="cb16-4"><a href="#cb16-4" tabindex="-1"></a>dend <span class="ot">&lt;-</span> <span class="fu">as.dendrogram</span>(<span class="fu">hclust</span>(<span class="fu">dist</span>(USArrests[<span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>,])))</span>
<span id="cb16-5"><a href="#cb16-5" tabindex="-1"></a><span class="co"># Like: </span></span>
<span id="cb16-6"><a href="#cb16-6" tabindex="-1"></a><span class="co"># dend &lt;- USArrests[1:5,] %&gt;% dist %&gt;% hclust %&gt;% as.dendrogram</span></span>
<span id="cb16-7"><a href="#cb16-7" tabindex="-1"></a></span>
<span id="cb16-8"><a href="#cb16-8" tabindex="-1"></a>dend1 <span class="ot">&lt;-</span> <span class="fu">color_branches</span>(dend, <span class="at">k =</span> <span class="dv">3</span>)</span>
<span id="cb16-9"><a href="#cb16-9" tabindex="-1"></a>dend2 <span class="ot">&lt;-</span> <span class="fu">color_labels</span>(dend, <span class="at">k =</span> <span class="dv">3</span>)</span>
<span id="cb16-10"><a href="#cb16-10" tabindex="-1"></a></span>
<span id="cb16-11"><a href="#cb16-11" tabindex="-1"></a><span class="fu">par</span>(<span class="at">mfrow =</span> <span class="fu">c</span>(<span class="dv">1</span>,<span class="dv">2</span>))</span>
<span id="cb16-12"><a href="#cb16-12" tabindex="-1"></a><span class="fu">plot</span>(dend1, <span class="at">main =</span> <span class="st">&quot;Colored branches&quot;</span>)</span>
<span id="cb16-13"><a href="#cb16-13" tabindex="-1"></a><span class="fu">plot</span>(dend2, <span class="at">main =</span> <span class="st">&quot;Colored labels&quot;</span>)</span></code></pre></div>
<p><img role="img" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAlgAAADhCAYAAAAUPMtIAAAEDmlDQ1BrQ0dDb2xvclNwYWNlR2VuZXJpY1JHQgAAOI2NVV1oHFUUPpu5syskzoPUpqaSDv41lLRsUtGE2uj+ZbNt3CyTbLRBkMns3Z1pJjPj/KRpKT4UQRDBqOCT4P9bwSchaqvtiy2itFCiBIMo+ND6R6HSFwnruTOzu5O4a73L3PnmnO9+595z7t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sZfGTvVH7cO9YesHjdkrDDGLLX5KXX8bdOmjaNSpUr6niFDhqR+fdKXKyc5/qSicfkHuLwEFpBrAr/99pv+IfHx8bnhs/JLu1ChQvp+NTuj75cfOhFE8oO4aNEife7aHzL1F5G+Lj9s6q8dhwgYQ3SIaJFk/ICLIJF7DLFniDFl9K3vE2ElZd1xxx36+NVXX9XH8pyyH9PnjB/qnAisd999Vz8jdTLqPWvWrFSBJWW99tprDvWXsCM2NlYLFzWD55g0aZJ+Tv7p0KGDrsNXX32lzxkCq1WrVvo5OXnXXXfpeyZOnKjvMQY8tXyhj5URrkPNvukBSeqSXmAdPnxY3yP5S30kX0ky8MmxiENhJkkGOzn34Ycf6mPpGxGJ8+bNc6iZL8fOnTu1uBJhykQCBZ2AO45v0qfGWCYCS8btG41Zxvgrf6CKIJM0aNAgPZbI+C3p8ccf18dqxlwfq1ULh1rp0H+Ayu8BSenLlWOOP0LBnMQlQvWbz25Jdn1ISkhIuOGOu
</div>
<div id="change-dendrograms-labels" class="section level3">
<h3>Change dendrograms labels</h3>
<p>Use the left assign <code>labels&lt;-</code> function:</p>
<div class="sourceCode" id="cb17"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" tabindex="-1"></a><span class="co"># install.packages(&quot;dendextend&quot;)</span></span>
<span id="cb17-2"><a href="#cb17-2" tabindex="-1"></a><span class="fu">library</span>(dendextend)</span>
<span id="cb17-3"><a href="#cb17-3" tabindex="-1"></a></span>
<span id="cb17-4"><a href="#cb17-4" tabindex="-1"></a>dend <span class="ot">&lt;-</span> <span class="fu">as.dendrogram</span>(<span class="fu">hclust</span>(<span class="fu">dist</span>(USArrests[<span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>,])))</span>
<span id="cb17-5"><a href="#cb17-5" tabindex="-1"></a><span class="co"># Like: </span></span>
<span id="cb17-6"><a href="#cb17-6" tabindex="-1"></a><span class="co"># dend &lt;- USArrests[1:5,] %&gt;% dist %&gt;% hclust %&gt;% as.dendrogram</span></span>
<span id="cb17-7"><a href="#cb17-7" tabindex="-1"></a></span>
<span id="cb17-8"><a href="#cb17-8" tabindex="-1"></a><span class="fu">labels</span>(dend)</span></code></pre></div>
<pre><code>## [1] &quot;Arkansas&quot; &quot;Arizona&quot; &quot;California&quot; &quot;Alabama&quot; &quot;Alaska&quot;</code></pre>
<div class="sourceCode" id="cb19"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" tabindex="-1"></a><span class="fu">labels</span>(dend) <span class="ot">&lt;-</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">5</span></span>
<span id="cb19-2"><a href="#cb19-2" tabindex="-1"></a><span class="fu">labels</span>(dend)</span></code></pre></div>
<pre><code>## [1] 1 2 3 4 5</code></pre>
</div>
<div id="larger-font-for-leaves-in-a-dendrogram" class="section level3">
<h3>Larger font for leaves in a dendrogram</h3>
<p>Asked (<a href="https://stackoverflow.com/questions/26965390/larger-font-and-spacing-between-leaves-in-r-dendrogram" class="uri">https://stackoverflow.com/questions/26965390/larger-font-and-spacing-between-leaves-in-r-dendrogram</a>)[here].</p>
<p>Solution: use the <code>set</code> function, with the “labels_cex”
parameter.</p>
<div class="sourceCode" id="cb21"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" tabindex="-1"></a><span class="co"># install.packages(&quot;dendextend&quot;)</span></span>
<span id="cb21-2"><a href="#cb21-2" tabindex="-1"></a><span class="fu">library</span>(dendextend)</span>
<span id="cb21-3"><a href="#cb21-3" tabindex="-1"></a></span>
<span id="cb21-4"><a href="#cb21-4" tabindex="-1"></a>dend <span class="ot">&lt;-</span> <span class="fu">as.dendrogram</span>(<span class="fu">hclust</span>(<span class="fu">dist</span>(USArrests[<span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>,])))</span>
<span id="cb21-5"><a href="#cb21-5" tabindex="-1"></a><span class="co"># Like: </span></span>
<span id="cb21-6"><a href="#cb21-6" tabindex="-1"></a><span class="co"># dend &lt;- USArrests[1:5,] %&gt;% dist %&gt;% hclust %&gt;% as.dendrogram</span></span>
<span id="cb21-7"><a href="#cb21-7" tabindex="-1"></a></span>
<span id="cb21-8"><a href="#cb21-8" tabindex="-1"></a><span class="co"># By default, the dend has no text size to it (showing only the first leaf)</span></span>
<span id="cb21-9"><a href="#cb21-9" tabindex="-1"></a><span class="fu">get_leaves_nodePar</span>(dend)[[<span class="dv">1</span>]]</span></code></pre></div>
<pre><code>## [1] NA</code></pre>
<div class="sourceCode" id="cb23"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" tabindex="-1"></a><span class="fu">par</span>(<span class="at">mfrow =</span> <span class="fu">c</span>(<span class="dv">1</span>,<span class="dv">2</span>), <span class="at">mar =</span> <span class="fu">c</span>(<span class="dv">10</span>,<span class="dv">4</span>,<span class="dv">4</span>,<span class="dv">2</span>))</span>
<span id="cb23-2"><a href="#cb23-2" tabindex="-1"></a><span class="fu">plot</span>(dend, <span class="at">main =</span> <span class="st">&quot;Original dend&quot;</span>)</span>
<span id="cb23-3"><a href="#cb23-3" tabindex="-1"></a></span>
<span id="cb23-4"><a href="#cb23-4" tabindex="-1"></a><span class="co"># let&#39;s increase the size of the labels:</span></span>
<span id="cb23-5"><a href="#cb23-5" tabindex="-1"></a>dend <span class="ot">&lt;-</span> <span class="fu">set</span>(dend, <span class="st">&quot;labels_cex&quot;</span>, <span class="dv">2</span>)</span>
<span id="cb23-6"><a href="#cb23-6" tabindex="-1"></a><span class="co"># Now each state has a larger label</span></span>
<span id="cb23-7"><a href="#cb23-7" tabindex="-1"></a><span class="fu">get_leaves_nodePar</span>(dend)[[<span class="dv">1</span>]]</span></code></pre></div>
<pre><code>## $lab.cex
## [1] 2
##
## $pch
## [1] NA</code></pre>
<div class="sourceCode" id="cb25"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" tabindex="-1"></a><span class="fu">plot</span>(dend, <span class="at">main =</span> <span class="st">&quot;A larger font for labels&quot;</span>)</span></code></pre></div>
<p><img role="img" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAlgAAAEsCAYAAAAfPc2WAAAEDmlDQ1BrQ0dDb2xvclNwYWNlR2VuZXJpY1JHQgAAOI2NVV1oHFUUPpu5syskzoPUpqaSDv41lLRsUtGE2uj+ZbNt3CyTbLRBkMns3Z1pJjPj/KRpKT4UQRDBqOCT4P9bwSchaqvtiy2itFCiBIMo+ND6R6HSFwnruTOzu5O4a73L3PnmnO9+595z7t4LkLgsW5beJQIsGq4t5dPis8fmxMQ6dMF90A190C0rjpUqlSYBG+PCv9rt7yDG3tf2t/f/Z+uuUEcBiN2F2Kw4yiLiZQD+FcWyXYAEQfvICddi+AnEO2ycIOISw7UAVxieD/Cyz5mRMohfRSwoqoz+xNuIB+cj9loEB3Pw2448NaitKSLLRck2q5pOI9O9g/t/tkXda8Tbg0+PszB9FN8DuPaXKnKW4YcQn1Xk3HSIry5ps8UQ/2W5aQnxIwBdu7yFcgrxPsRjVXu8HOh0qao30cArp9SZZxDfg3h1wTzKxu5E/LUxX5wKdX5SnAzmDx4A4OIqLbB69yMesE1pKojLjVdoNsfyiPi45hZmAn3uLWdpOtfQOaVmikEs7ovj8hFWpz7EV6mel0L9Xy23FMYlPYZenAx0yDB1/PX6dledmQjikjkXCxqMJS9WtfFCyH9XtSekEF+2dH+P4tzITduTygGfv58a5VCTH5PtXD7EFZiNyUDBhHnsFTBgE0SQIA9pfFtgo6cKGuhooeilaKH41eDs38Ip+f4At1Rq/sjr6NEwQqb/I/DQqsLvaFUjvAx+eWirddAJZnAj1DFJL0mSg/gcIpPkMBkhoyCSJ8lTZIxk0TpKDjXHliJzZPO50dR5ASNSnzeLvIvod0HG/mdkmOC0z8VKnzcQ2M/Yz2vKldduXjp9bleLu0ZWn7vWc+l0JGcaai10yNrUnXLP/8Jf59ewX+c3Wgz+B34Df+vbVrc16zTMVgp9um9bxEfzPU5kPqUtVWxhs6OiWTVW+gIfywB9uX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AiIgBUufAREQAREQAREQARHImIAUrIyBqjkREAEREAEREAERkIKlz4AIiIAIiIAIiIAIZExAClbGQNWcCIiACIiACIiACEjB6gWfgZkzZ4aXX365F1ypLlEEREAE6ifw3//+N0yZMiXwv1Xy0ksvBcZoSc8jIAWr591Tv6JPP/00HH/88WGJJZYI8803X1h00UXDvPPOG7bffvvw73//u66rXmCBBUKfPn3CCy+8UNf+7LTvvvv6Maeddlrdx9SzY1f6cuyxx3pfDj/88HpO0a19lllmGT/XX//61261o4NFoJUELrvsMv8c872//PLLa3blzjvv9P2/9a1v1dy3aDuce+654Ytf/GJYcskl/f/HH3+caRcvvvji8OSTT1Zt85lnngmLLbaYvxibr7jiiqr7dmVDo2PxhAkT/F5usskmXTld6ZhGz1s6sAcuSMHqgTeVS9puu+3CCSecEF588UVXslZZZZXAAMKg+bWvfS1MnTq15pXPMcccgReDbb0y++yz+zGzzaaPVr3MtJ8IFIXAhRdeWOrKBRdcUFruiQs//elPfUzcY489wi9/+cvQt2/fzC5z6623DjvttFN4++23q7YJX6xXKKc///nPw+qrr151X21oTwL6FWzP+9Zpr//yl7+Ea665xhWj3/72t+H5558PDz74YHj66afD0ksvHd56661w9NFHd9oGG7F0YTrHClav/OY3v/Fj8rAa1dsn7ScCIlCbAG4Et956a5hzzjkDD0i33XabP6DVPrL99kiSpOQ2ceqpp4b9998/04t47rnnaraHcoWg4P3whz8MQ4YMqXmMdmgvAlKw2ut+1dVbzPzItttu61/eeBCK0llnneVvL7300vD666+Hzz//PCy33HJhvfXWC5jMBw0aFKKJeK211vJtaf+tc845JwwdOjQwZTdmzJjwpz/9yff59a9/7e2efPLJ/v53v/udv3/ggQf8PQrdeeed59azL33pSz5V+eabb8auhauuuipsvvnm3i5m84033jhgQm9EuDb6Nv/884ddd921ol/DP/7xD79WTPLse8wxx3Twv1hzzTX9ifLxxx8P66yzjk+rrrzyyuG+++4rdeXdd9/1a4fVUkst5UwZsCUi0M4Efv/73/t48L3vfS+su+66gc90/B43cl3//Oc/w+677+7fr4EDB7plhrajHHrooT4mMAYNGzbMH/qeeOIJ31xrfGGn8ePHh69//es+rYdl/s9//nNsOtx0003eNuPQNtts4+PZuHHjSttZwOeKMY+xD1l77bXD2LFjffnDDz90ZWeFFVbw7/4aa6wReGBNS60xYueddw4wQHbZZZfwox/9KH24L2Phoq/IKaec4mONv7E/f/jDH8KGG27orh3Dhw8PP/nJT8Inn3wSN4fO+JV2qrDw2WefhR//+MeB/jM1CkMUy/KpUfY77rjjXOFD6UP5S+/z3nvvhX322ce3L7jggmHLLbcM06ZNq3DG/1uFuwpTsf369QsLLbRQ2HHHHSuOz/93RA9Zsi+RpIcR+OY3v8mvfWLKzyxXZoNKYh9y337PPfck9mXyZXtqTWx6LzHFIxk9erQfZ4qKbzMLmL+3L76/p21TyJKFF144mWuuuXzdEUcc4fvYF8/f21Ohv7/lllv8Pe2yv01dJuYT5ut+8IMf+D7PPvus94l9dthhh2T55Zf37faF9O38Ke9LacP/Fi666CI/hr7Zj4Ofy6Y2fZ0NEL6XDQLJ3HPP7etMeUpon/132223UnP0wZ7eE1Mgk+9///uJDXC+zyKLLJKYX5vvZwqor6Mtlr/whS8k8VwPPfRQqS0tiEA7EfjqV7/qn+urr746id91+4Etfe4rXcsdd9zhx6y44oq+mfHEprp8nT0k+ffD3Az8/d/+9jffZ6uttvL3/fv3T+yH3seDj
<p>(Note that changing the spacing between the labels is currently not
implemented)</p>
</div>
<div id="how-to-view-attributes-of-a-dendrogram" class="section level3">
<h3>How to view attributes of a dendrogram</h3>
<p>Asked (<a href="https://stackoverflow.com/questions/26240200/how-to-access-attributes-of-a-dendrogram-in-r" class="uri">https://stackoverflow.com/questions/26240200/how-to-access-attributes-of-a-dendrogram-in-r</a>)[here],
and (<a href="https://stackoverflow.com/questions/25664911/r-hclust-height-of-final-merge" class="uri">https://stackoverflow.com/questions/25664911/r-hclust-height-of-final-merge</a>)[here].</p>
<p>It generally depends on which attribute we want to view. For
“midpoint” (or height) use the <code>get_nodes_attr</code> function,
with the “midpoint” parameter.</p>
<div class="sourceCode" id="cb26"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" tabindex="-1"></a><span class="co"># install.packages(&quot;dendextend&quot;)</span></span>
<span id="cb26-2"><a href="#cb26-2" tabindex="-1"></a><span class="fu">library</span>(dendextend)</span>
<span id="cb26-3"><a href="#cb26-3" tabindex="-1"></a></span>
<span id="cb26-4"><a href="#cb26-4" tabindex="-1"></a>dend <span class="ot">&lt;-</span> <span class="fu">as.dendrogram</span>(<span class="fu">hclust</span>(<span class="fu">dist</span>(USArrests[<span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>,])))</span>
<span id="cb26-5"><a href="#cb26-5" tabindex="-1"></a><span class="co"># Like: </span></span>
<span id="cb26-6"><a href="#cb26-6" tabindex="-1"></a><span class="co"># dend &lt;- USArrests[1:5,] %&gt;% dist %&gt;% hclust %&gt;% as.dendrogram</span></span>
<span id="cb26-7"><a href="#cb26-7" tabindex="-1"></a></span>
<span id="cb26-8"><a href="#cb26-8" tabindex="-1"></a><span class="co"># midpoint for all nodes</span></span>
<span id="cb26-9"><a href="#cb26-9" tabindex="-1"></a><span class="fu">get_nodes_attr</span>(dend, <span class="st">&quot;midpoint&quot;</span>)</span></code></pre></div>
<pre><code>## [1] 1.25 NA 1.50 0.50 NA NA 0.50 NA NA</code></pre>
<div class="sourceCode" id="cb28"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb28-1"><a href="#cb28-1" tabindex="-1"></a><span class="co"># Perhaps also for the height:</span></span>
<span id="cb28-2"><a href="#cb28-2" tabindex="-1"></a><span class="fu">get_nodes_attr</span>(dend, <span class="st">&quot;height&quot;</span>)</span></code></pre></div>
<pre><code>## [1] 108.85192 0.00000 63.00833 23.19418 0.00000 0.00000 37.17701
## [8] 0.00000 0.00000</code></pre>
<p>To also change an attribute, you can use the various assign functions
from the package: <code>assign_values_to_leaves_nodePar</code>,
<code>assign_values_to_leaves_edgePar</code>,
<code>assign_values_to_nodes_nodePar</code>,
<code>assign_values_to_branches_edgePar</code>,
<code>remove_branches_edgePar</code>,
<code>remove_nodes_nodePar</code></p>
</div>
<div id="how-to-color-the-branches-in-heatmap.2" class="section level3">
<h3>How to color the branches in heatmap.2?</h3>
<p>Asked (<a href="https://stackoverflow.com/questions/29265536/how-to-color-the-branches-and-tick-labels-in-the-heatmap-2" class="uri">https://stackoverflow.com/questions/29265536/how-to-color-the-branches-and-tick-labels-in-the-heatmap-2</a>?)[here].</p>
<p>Solution: use the <code>color_branches</code> function (or the
<code>set</code> function, with the “branches_k_color”, “k”, and “value”
parameters).</p>
<p>(Getting the data for this example is from the (<a href="https://stackoverflow.com/questions/29265536/how-to-color-the-branches-and-tick-labels-in-the-heatmap-2" class="uri">https://stackoverflow.com/questions/29265536/how-to-color-the-branches-and-tick-labels-in-the-heatmap-2</a>)[original
SO question])</p>
<div class="sourceCode" id="cb30"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb30-1"><a href="#cb30-1" tabindex="-1"></a>test <span class="ot">&lt;-</span> test0</span>
<span id="cb30-2"><a href="#cb30-2" tabindex="-1"></a>rnames <span class="ot">&lt;-</span> test[,<span class="dv">1</span>] </span>
<span id="cb30-3"><a href="#cb30-3" tabindex="-1"></a>test <span class="ot">&lt;-</span> <span class="fu">data.matrix</span>(test[,<span class="dv">2</span><span class="sc">:</span><span class="fu">ncol</span>(test)]) <span class="co"># to matrix</span></span>
<span id="cb30-4"><a href="#cb30-4" tabindex="-1"></a><span class="fu">rownames</span>(test) <span class="ot">&lt;-</span> rnames </span>
<span id="cb30-5"><a href="#cb30-5" tabindex="-1"></a>test <span class="ot">&lt;-</span> <span class="fu">scale</span>(test, <span class="at">center=</span>T, <span class="at">scale=</span>T) <span class="co"># data standarization</span></span>
<span id="cb30-6"><a href="#cb30-6" tabindex="-1"></a>test <span class="ot">&lt;-</span> <span class="fu">t</span>(test) <span class="co"># transpose</span></span>
<span id="cb30-7"><a href="#cb30-7" tabindex="-1"></a></span>
<span id="cb30-8"><a href="#cb30-8" tabindex="-1"></a></span>
<span id="cb30-9"><a href="#cb30-9" tabindex="-1"></a><span class="do">## Creating a color palette &amp; color breaks</span></span>
<span id="cb30-10"><a href="#cb30-10" tabindex="-1"></a></span>
<span id="cb30-11"><a href="#cb30-11" tabindex="-1"></a>my_palette <span class="ot">&lt;-</span> <span class="fu">colorRampPalette</span>(<span class="fu">c</span>(<span class="st">&quot;forestgreen&quot;</span>, <span class="st">&quot;yellow&quot;</span>, <span class="st">&quot;red&quot;</span>))(<span class="at">n =</span> <span class="dv">299</span>)</span>
<span id="cb30-12"><a href="#cb30-12" tabindex="-1"></a></span>
<span id="cb30-13"><a href="#cb30-13" tabindex="-1"></a>col_breaks <span class="ot">=</span> <span class="fu">c</span>(<span class="fu">seq</span>(<span class="sc">-</span><span class="dv">1</span>,<span class="sc">-</span><span class="fl">0.5</span>,<span class="at">length=</span><span class="dv">100</span>), <span class="co"># forestgreen</span></span>
<span id="cb30-14"><a href="#cb30-14" tabindex="-1"></a> <span class="fu">seq</span>(<span class="sc">-</span><span class="fl">0.5</span>,<span class="fl">0.5</span>,<span class="at">length=</span><span class="dv">100</span>), <span class="co"># yellow</span></span>
<span id="cb30-15"><a href="#cb30-15" tabindex="-1"></a> <span class="fu">seq</span>(<span class="fl">0.5</span>,<span class="dv">1</span>,<span class="at">length=</span><span class="dv">100</span>)) <span class="co"># red</span></span>
<span id="cb30-16"><a href="#cb30-16" tabindex="-1"></a></span>
<span id="cb30-17"><a href="#cb30-17" tabindex="-1"></a><span class="co"># distance &amp; hierarchical clustering</span></span>
<span id="cb30-18"><a href="#cb30-18" tabindex="-1"></a>distance <span class="ot">=</span> <span class="fu">dist</span>(test, <span class="at">method =</span><span class="st">&quot;euclidean&quot;</span>) </span>
<span id="cb30-19"><a href="#cb30-19" tabindex="-1"></a>hcluster <span class="ot">=</span> <span class="fu">hclust</span>(distance, <span class="at">method =</span><span class="st">&quot;ward.D&quot;</span>)</span>
<span id="cb30-20"><a href="#cb30-20" tabindex="-1"></a></span>
<span id="cb30-21"><a href="#cb30-21" tabindex="-1"></a></span>
<span id="cb30-22"><a href="#cb30-22" tabindex="-1"></a>dend1 <span class="ot">&lt;-</span> <span class="fu">as.dendrogram</span>(hcluster)</span>
<span id="cb30-23"><a href="#cb30-23" tabindex="-1"></a></span>
<span id="cb30-24"><a href="#cb30-24" tabindex="-1"></a><span class="co"># Get the dendextend package</span></span>
<span id="cb30-25"><a href="#cb30-25" tabindex="-1"></a><span class="cf">if</span>(<span class="sc">!</span><span class="fu">require</span>(dendextend)) <span class="fu">install.packages</span>(<span class="st">&quot;dendextend&quot;</span>)</span>
<span id="cb30-26"><a href="#cb30-26" tabindex="-1"></a><span class="fu">library</span>(dendextend)</span>
<span id="cb30-27"><a href="#cb30-27" tabindex="-1"></a><span class="co"># get some colors</span></span>
<span id="cb30-28"><a href="#cb30-28" tabindex="-1"></a>cols_branches <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="st">&quot;darkred&quot;</span>, <span class="st">&quot;forestgreen&quot;</span>, <span class="st">&quot;orange&quot;</span>, <span class="st">&quot;blue&quot;</span>)</span>
<span id="cb30-29"><a href="#cb30-29" tabindex="-1"></a><span class="co"># Set the colors of 4 branches</span></span>
<span id="cb30-30"><a href="#cb30-30" tabindex="-1"></a>dend1 <span class="ot">&lt;-</span> <span class="fu">color_branches</span>(dend1, <span class="at">k =</span> <span class="dv">4</span>, <span class="at">col =</span> cols_branches)</span>
<span id="cb30-31"><a href="#cb30-31" tabindex="-1"></a><span class="co"># or with:</span></span>
<span id="cb30-32"><a href="#cb30-32" tabindex="-1"></a><span class="co"># dend1 &lt;- set(dend1, &quot;branches_k_color&quot;, k = 4, value = cols_branches)</span></span>
<span id="cb30-33"><a href="#cb30-33" tabindex="-1"></a></span>
<span id="cb30-34"><a href="#cb30-34" tabindex="-1"></a><span class="co"># Get the colors of the tips of the dendrogram:</span></span>
<span id="cb30-35"><a href="#cb30-35" tabindex="-1"></a><span class="co"># col_labels &lt;- cols_branches[cutree(dend1, k = 4)] # this may need tweaking in various cases - the following is a more general solution.</span></span>
<span id="cb30-36"><a href="#cb30-36" tabindex="-1"></a></span>
<span id="cb30-37"><a href="#cb30-37" tabindex="-1"></a>col_labels <span class="ot">&lt;-</span> <span class="fu">get_leaves_branches_col</span>(dend1)</span>
<span id="cb30-38"><a href="#cb30-38" tabindex="-1"></a><span class="co"># But due to the way heatmap.2 works - we need to fix it to be in the </span></span>
<span id="cb30-39"><a href="#cb30-39" tabindex="-1"></a><span class="co"># order of the data! </span></span>
<span id="cb30-40"><a href="#cb30-40" tabindex="-1"></a>col_labels <span class="ot">&lt;-</span> col_labels[<span class="fu">order</span>(<span class="fu">order.dendrogram</span>(dend1))]</span>
<span id="cb30-41"><a href="#cb30-41" tabindex="-1"></a></span>
<span id="cb30-42"><a href="#cb30-42" tabindex="-1"></a>dend1</span>
<span id="cb30-43"><a href="#cb30-43" tabindex="-1"></a><span class="co"># plot(dend1)</span></span>
<span id="cb30-44"><a href="#cb30-44" tabindex="-1"></a><span class="co"># a &lt;- heights_per_k.dendrogram(dend1)</span></span>
<span id="cb30-45"><a href="#cb30-45" tabindex="-1"></a><span class="co"># a2 &lt;- heights_per_k.dendrogram(dend1)</span></span>
<span id="cb30-46"><a href="#cb30-46" tabindex="-1"></a><span class="co"># nleaves(dend1)</span></span>
<span id="cb30-47"><a href="#cb30-47" tabindex="-1"></a></span>
<span id="cb30-48"><a href="#cb30-48" tabindex="-1"></a></span>
<span id="cb30-49"><a href="#cb30-49" tabindex="-1"></a><span class="co"># Creating Heat Map</span></span>
<span id="cb30-50"><a href="#cb30-50" tabindex="-1"></a><span class="co"># if(!require(gplots)) install.packages(&quot;gplots&quot;)</span></span>
<span id="cb30-51"><a href="#cb30-51" tabindex="-1"></a><span class="fu">library</span>(gplots)</span>
<span id="cb30-52"><a href="#cb30-52" tabindex="-1"></a><span class="fu">heatmap.2</span>(test, </span>
<span id="cb30-53"><a href="#cb30-53" tabindex="-1"></a> <span class="at">main =</span> <span class="fu">paste</span>( <span class="st">&quot;test&quot;</span>), </span>
<span id="cb30-54"><a href="#cb30-54" tabindex="-1"></a> <span class="at">trace=</span><span class="st">&quot;none&quot;</span>, </span>
<span id="cb30-55"><a href="#cb30-55" tabindex="-1"></a> <span class="at">margins =</span><span class="fu">c</span>(<span class="dv">5</span>,<span class="dv">7</span>), </span>
<span id="cb30-56"><a href="#cb30-56" tabindex="-1"></a> <span class="at">col=</span>my_palette, </span>
<span id="cb30-57"><a href="#cb30-57" tabindex="-1"></a> <span class="at">breaks=</span>col_breaks, </span>
<span id="cb30-58"><a href="#cb30-58" tabindex="-1"></a> <span class="at">dendrogram=</span><span class="st">&quot;row&quot;</span>, </span>
<span id="cb30-59"><a href="#cb30-59" tabindex="-1"></a> <span class="at">Rowv =</span> dend1, </span>
<span id="cb30-60"><a href="#cb30-60" tabindex="-1"></a> <span class="at">Colv =</span> <span class="st">&quot;NA&quot;</span>, </span>
<span id="cb30-61"><a href="#cb30-61" tabindex="-1"></a> <span class="at">key.xlab =</span> <span class="st">&quot;Concentration (index)&quot;</span>,</span>
<span id="cb30-62"><a href="#cb30-62" tabindex="-1"></a> <span class="at">cexRow =</span><span class="fl">0.6</span>,</span>
<span id="cb30-63"><a href="#cb30-63" tabindex="-1"></a> <span class="at">cexCol =</span> <span class="fl">0.8</span>,</span>
<span id="cb30-64"><a href="#cb30-64" tabindex="-1"></a> <span class="at">na.rm =</span> <span class="cn">TRUE</span>,</span>
<span id="cb30-65"><a href="#cb30-65" tabindex="-1"></a> <span class="at">RowSideColors =</span> col_labels <span class="co"># to add nice colored strips </span></span>
<span id="cb30-66"><a href="#cb30-66" tabindex="-1"></a> <span class="co"># colRow = col_labels # to add nice colored labels - only for qplots 2.17.0 and higher</span></span>
<span id="cb30-67"><a href="#cb30-67" tabindex="-1"></a> ) </span></code></pre></div>
</div>
<div id="for-package-developers---how-to-call-imported-calls-from-dendextend-0.18.3" class="section level3">
<h3>For package developers - how to call imported calls from dendextend
0.18.3?</h3>
<p>If you are developing a package and you wish to use dendextend as an
imported package, that is - without loading it to the search path, you
should run:</p>
<div class="sourceCode" id="cb31"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb31-1"><a href="#cb31-1" tabindex="-1"></a>dendextend<span class="sc">::</span><span class="fu">assign_dendextend_options</span>()</span>
<span id="cb31-2"><a href="#cb31-2" tabindex="-1"></a><span class="co"># This populates the dendextend::dendextend_options() space</span></span></code></pre></div>
<p>Before using any of its function (for example:
<code>dendextend::color_branches</code> ). As of dendextend version
1.0.0, this is no longer required.</p>
</div>
<div id="how-to-plot-a-fan-polar-dendrogram-in-r" class="section level3">
<h3>How to plot a fan (Polar) Dendrogram in R?</h3>
<p>Asked (<a href="https://stats.stackexchange.com/questions/4062/how-to-plot-a-fan-polar-dendrogram-in-r" class="uri">https://stats.stackexchange.com/questions/4062/how-to-plot-a-fan-polar-dendrogram-in-r</a>)[here].</p>
<p>Solution: use the <code>circlize_dendrogram</code> function.</p>
<div class="sourceCode" id="cb32"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb32-1"><a href="#cb32-1" tabindex="-1"></a><span class="co"># install.packages(&quot;dendextend&quot;)</span></span>
<span id="cb32-2"><a href="#cb32-2" tabindex="-1"></a><span class="co"># install.packages(&quot;circlize&quot;)</span></span>
<span id="cb32-3"><a href="#cb32-3" tabindex="-1"></a><span class="fu">library</span>(dendextend)</span>
<span id="cb32-4"><a href="#cb32-4" tabindex="-1"></a><span class="fu">library</span>(circlize)</span>
<span id="cb32-5"><a href="#cb32-5" tabindex="-1"></a></span>
<span id="cb32-6"><a href="#cb32-6" tabindex="-1"></a><span class="co"># create a dendrogram</span></span>
<span id="cb32-7"><a href="#cb32-7" tabindex="-1"></a>hc <span class="ot">&lt;-</span> <span class="fu">hclust</span>(<span class="fu">dist</span>(datasets<span class="sc">::</span>mtcars))</span>
<span id="cb32-8"><a href="#cb32-8" tabindex="-1"></a>dend <span class="ot">&lt;-</span> <span class="fu">as.dendrogram</span>(hc)</span>
<span id="cb32-9"><a href="#cb32-9" tabindex="-1"></a></span>
<span id="cb32-10"><a href="#cb32-10" tabindex="-1"></a><span class="co"># modify the dendrogram to have some colors in the branches and labels</span></span>
<span id="cb32-11"><a href="#cb32-11" tabindex="-1"></a>dend <span class="ot">&lt;-</span> dend <span class="sc">%&gt;%</span> </span>
<span id="cb32-12"><a href="#cb32-12" tabindex="-1"></a> <span class="fu">color_branches</span>(<span class="at">k=</span><span class="dv">4</span>) <span class="sc">%&gt;%</span> </span>
<span id="cb32-13"><a href="#cb32-13" tabindex="-1"></a> color_labels</span>
<span id="cb32-14"><a href="#cb32-14" tabindex="-1"></a></span>
<span id="cb32-15"><a href="#cb32-15" tabindex="-1"></a><span class="co"># plot the radial plot</span></span>
<span id="cb32-16"><a href="#cb32-16" tabindex="-1"></a><span class="fu">par</span>(<span class="at">mar =</span> <span class="fu">rep</span>(<span class="dv">0</span>,<span class="dv">4</span>))</span>
<span id="cb32-17"><a href="#cb32-17" tabindex="-1"></a><span class="co"># circlize_dendrogram(dend, dend_track_height = 0.8) </span></span>
<span id="cb32-18"><a href="#cb32-18" tabindex="-1"></a><span class="fu">circlize_dendrogram</span>(dend, <span class="at">labels_track_height =</span> <span class="cn">NA</span>, <span class="at">dend_track_height =</span> .<span class="dv">3</span>) </span></code></pre></div>
<p><img role="img" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAu4AAALuCAYAAADxHZPKAAAEDmlDQ1BrQ0dDb2xvclNwYWNlR2VuZXJpY1JHQgAAOI2NVV1oHFUUPpu5syskzoPUpqaSDv41lLRsUtGE2uj+ZbNt3CyTbLRBkMns3Z1pJjPj/KRpKT4UQRDBqOCT4P9bwSchaqvtiy2itFCiBIMo+ND6R6HSFwnruTOzu5O4a73L3PnmnO9+595z7t4LkLgsW5beJQIsGq4t5dPis8fmxMQ6dMF90A190C0rjpUqlSYBG+PCv9rt7yDG3tf2t/f/Z+uuUEcBiN2F2Kw4yiL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xrPqx/r9Vt4biXeANh8vV7/+/16ffMD1N8AuKD/A/8leAvFY9bLAAAAOGVYSWZNTQAqAAAACAABh2kABAAAAAEAAAAaAAAAAAACoAIABAAAAAEAAALuoAMABAAAAAEAAALuAAAAADeZinEAAEAASURBVHgB7J0HYFRV9sY/SJ1k0nslvZAACb13bDQVFbEi6trbf+11dVdd29obdsUuKkVFpYl0CAkBEpKQ3ntvk8L/nBcTEooECMm8l3N3x3kzmXLv776Q75537ncGHKYGaUJACAgBISAEhIAQEAJCQAgYNYGBRt076ZwQEAJCQAgIASEgBISAEBACCgER7nIiCAEhIASEgBAQAkJACAgBFRAQ4a6CSZIuCgEhIASEgBAQAkJACAgBEe5yDggBISAEhIAQEAJCQAgIARUQEOGugkmSLgoBISAEhIAQEAJCQAgIARHucg4IASEgBISAEBACQkAICAEVEBDhroJJki4KASEgBISAEBACQkAICAER7nIOCAEhIASEgBAQAkJACAgBFRAQ4a6CSZIuCgEhIASEgBAQAkJACAgBEe5yDggBISAEhIAQEAJCQAgIARUQEOGugkmSLgoBISAEhIAQEAJCQAgIARHucg4IASEgBISAEBACQkAICAEVEBDhroJJki4KASEgBISAEBACQkAICAER7nIOCAEhIASEgBAQAkJACAgBFRAQ4a6CSZIuCgEhIASEgBAQAkJACAgBEe5yDggBISAEhIAQEAJCQAgIARUQEOGugkmSLgoBISAEhIAQEAJCQAgIARHucg4IASEgBISAEBACQkAICAEVEBDhroJJki4KASEgBISAEBACQkAICAER7nIOCAEhIASEgBAQAkJACAgBFRAQ4a6CSZIuCgEhIASEgBAQAkJACAgBEe5yDggBISAEhIAQEAJCQAgIARUQEOGugkmSLgoBISAEhIAQEAJCQAgIARHucg4IASEgBISAEBACQkAICAEVEBDhroJJki4KASEgBISAEBACQkAICAER7nIOCAEhIASEgBAQAkJACAgBFRAQ4a6CSZIuCgEhIASEgBAQAkJACAgBEe5yDggBISAEhIAQEAJCQAgIARUQEOGugkmSLgoBISAEhIAQEAJCQAgIARHucg4IASEgBISAEBACQkAICAEVEBDhroJJki4KASEgBISAEBACQkAICAER7nIOCAEhIASEgBAQAkJACAgBFRAQ4a6CSZIuCgEhIASEgBAQAkJACAgBEe5yDggBISAEhIAQEAJCQAgIARUQEOGugkmSLgoBISAEhIAQEAJCQAgIARHucg4IASEgBISAEBACQkAICAEVEBDhroJJki4KASEgBISAEBACQkAICAER7nIOCAEhIASEgBAQAkJACAgBFRAQ4a6CSZIuCgEhIASEgBAQAkJACAgBEe5yDggBISAEhIAQEAJCQAgIARUQEOGugkmSLgoBISAEhIAQEAJCQAgIARHucg4IASEgBISAEBACQkAICAEVEBDhroJJki4KASEgBISAEBACQkAICAER7nIOCAEhIASEgBAQAkJACAgBFRAQ4a6CSZIuCgEhIASEgBAQAkJACAgBEe5yDggBISAEhIAQEAJCQAgIARUQEOGugkmSLgoBISAEhIAQEAJCQAgIARHucg4IASEgBISAEBACQkAICAEVEBDhroJJki4KASEgBISAEBACQkAICAER7nIOCAEhIASEgBAQAkJACAgBFRAQ4a6CSZIuCgEhIASEgBAQAkJACAgBEe5yDggBISAEhIAQEAJCQAgIARUQEOGugkmSLgoBISAEhIAQEAJCQAgIARHucg4IASEgBISAEBACQkAICAEVEBDhroJJki4KASEgBISAEBACQkAICAER7nIOCAEhIASEgBAQAkJACAgBFRAQ4a6CSZIuCgEhIASEgBAQAkJACAgBEe5yDggBISAEhIAQEAJCQAgIARUQEOGugkmSLgoBISAEhIAQEAJCQAgIARHucg4IASEgBISAEBACQkAICAEVEBDhroJJki4KASEgBIRA3xOoKjKgvrq57zsiPRACQqDfEjDttyOXgQsBISAEhIAQ6CaBHd8V4tfXspVXuwXpEDjKDn7DbWDvYQEnHwsMGDCgm58kLxMCQkAInD6BAYepnf7b5Z1CQAgIASEgBLRNoKmxFa9eGo/Lnw1CSWYDEjaUobUFSNtdBY9QK9z43mBtA5DRCQEhYDQEJFXGaKZCOiIEhIAQEALGSKA0uwHuQVbwjtCDRbzvUBtc9b8Q+I+wwaLngo2xy9InISAENEpAhLtGJ1aGJQSEgBAQAj1DgLNgqooNaKxrQW1ZE8x0bX86XQOsUJRW3zNfIp8iBISAEOgGARHu3YAkLxECQkAICIH+S8At0AqO3hbIiK0GSMSzWGchf2hHpfJ8/yUjIxcCQqC3CUiOe28Tl+8TAkJACAgB1RE43Nq2Hayy0ICPbjuI6tImTFnsiSnXeapuLNJhISAE1EtAhLt65056LgSEgBAQAr1AgPPa6yubYetqrnxbs6GVXGQAEzO5aN0L+OUrhIAQ6ERA7CA7wZBDISAEhIAQOJZAGaWFbN9YjrysBrS0HIaNrSkGkSVi8GA9XD0tjn2Dhp4pSq/Hp3cmoY6Eu5W9KQJG2ZIVJN/so
</div>
<div id="a-way-to-calculate-lowest-value-of-h-in-cut-that-produces-groupings-of-a-given-minimum-size" class="section level3">
<h3>A way to calculate lowest value of h in cut that produces groupings
of a given minimum size?</h3>
<p>Asked (<a href="https://stackoverflow.com/questions/31124810/r-cut-dendrogram-into-groups-with-minimum-size/" class="uri">https://stackoverflow.com/questions/31124810/r-cut-dendrogram-into-groups-with-minimum-size/</a>)[here].</p>
<p>Solution: use the <code>heights_per_k.dendrogram</code> function.</p>
<p>This feature is available in the dendextend package with the
<code>heights_per_k.dendrogram</code> function (which also has a faster
C++ implementation when loading the dendextendRcpp function).</p>
<div class="sourceCode" id="cb33"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb33-1"><a href="#cb33-1" tabindex="-1"></a>hc <span class="ot">&lt;-</span> <span class="fu">hclust</span>(<span class="fu">dist</span>(USArrests[<span class="dv">1</span><span class="sc">:</span><span class="dv">4</span>,]), <span class="st">&quot;ave&quot;</span>)</span>
<span id="cb33-2"><a href="#cb33-2" tabindex="-1"></a>dend <span class="ot">&lt;-</span> <span class="fu">as.dendrogram</span>(hc)</span>
<span id="cb33-3"><a href="#cb33-3" tabindex="-1"></a><span class="fu">heights_per_k.dendrogram</span>(dend)</span></code></pre></div>
<pre><code>## 1 2 3 4
## 86.47086 68.84745 45.98871 28.36531</code></pre>
<p>As a sidenote, the dendextend package has a
<code>cutree.dendrogram</code> S3 method for dendrograms (which works
very similarly to <code>cutree</code> for <code>hclust</code>
objects).</p>
</div>
<div id="coloring-dendrograms-end-branches-or-leaves-based-on-column-number-of-data-frame-in-r" class="section level3">
<h3>Coloring dendrograms end branches (or leaves) based on column
number of data frame in R</h3>
<p>Asked (<a href="https://stackoverflow.com/questions/30062187/coloring-dendrogram-s-end-branches-or-leaves-based-on-column-number-of-data-fr" class="uri">https://stackoverflow.com/questions/30062187/coloring-dendrogram-s-end-branches-or-leaves-based-on-column-number-of-data-fr</a>)[here].</p>
<p>Solution: use the <code>assign_values_to_leaves_edgePar</code>
function.</p>
<div class="sourceCode" id="cb35"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb35-1"><a href="#cb35-1" tabindex="-1"></a>aa1 <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="dv">2</span>,<span class="dv">4</span>,<span class="dv">6</span>,<span class="dv">8</span>)</span>
<span id="cb35-2"><a href="#cb35-2" tabindex="-1"></a>bb1 <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="dv">1</span>,<span class="dv">3</span>,<span class="dv">7</span>,<span class="dv">11</span>)</span>
<span id="cb35-3"><a href="#cb35-3" tabindex="-1"></a>aa2 <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="dv">3</span>,<span class="dv">6</span>,<span class="dv">9</span>,<span class="dv">12</span>)</span>
<span id="cb35-4"><a href="#cb35-4" tabindex="-1"></a>bb2 <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="dv">3</span>,<span class="dv">5</span>,<span class="dv">7</span>,<span class="dv">9</span>)</span>
<span id="cb35-5"><a href="#cb35-5" tabindex="-1"></a>data.main <span class="ot">&lt;-</span> <span class="fu">data.frame</span>(aa1,bb1,aa2,bb2)</span>
<span id="cb35-6"><a href="#cb35-6" tabindex="-1"></a>d1 <span class="ot">&lt;-</span> <span class="fu">dist</span>(<span class="fu">t</span>(data.main))</span>
<span id="cb35-7"><a href="#cb35-7" tabindex="-1"></a>hcl1 <span class="ot">&lt;-</span> <span class="fu">hclust</span>(d1)</span>
<span id="cb35-8"><a href="#cb35-8" tabindex="-1"></a><span class="co"># plot(hcl1)</span></span>
<span id="cb35-9"><a href="#cb35-9" tabindex="-1"></a></span>
<span id="cb35-10"><a href="#cb35-10" tabindex="-1"></a>dend <span class="ot">&lt;-</span> <span class="fu">as.dendrogram</span>(hcl1)</span>
<span id="cb35-11"><a href="#cb35-11" tabindex="-1"></a>col_aa_red <span class="ot">&lt;-</span> <span class="fu">ifelse</span>(<span class="fu">grepl</span>(<span class="st">&quot;aa&quot;</span>, <span class="fu">labels</span>(dend)), <span class="st">&quot;red&quot;</span>, <span class="st">&quot;blue&quot;</span>)</span>
<span id="cb35-12"><a href="#cb35-12" tabindex="-1"></a>dend2 <span class="ot">&lt;-</span> <span class="fu">assign_values_to_leaves_edgePar</span>(<span class="at">dend=</span>dend, <span class="at">value =</span> col_aa_red, <span class="at">edgePar =</span> <span class="st">&quot;col&quot;</span>)</span>
<span id="cb35-13"><a href="#cb35-13" tabindex="-1"></a><span class="fu">plot</span>(dend2)</span></code></pre></div>
<p><img role="img" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcIAAAHCCAYAAAB8GMlFAAAEDmlDQ1BrQ0dDb2xvclNwYWNlR2VuZXJpY1JHQgAAOI2NVV1oHFUUPpu5syskzoPUpqaSDv41lLRsUtGE2uj+ZbNt3CyTbLRBkMns3Z1pJjPj/KRpKT4UQRDBqOCT4P9bwSchaqvtiy2itFCiBIMo+ND6R6HSFwnruTOzu5O4a73L3PnmnO9+595z7t4LkLgsW5beJQIsGq4t5dPis8fmxMQ6dMF90A190C0rjpUqlSYBG+PCv9rt7yDG3tf2t/f/Z+uuUEcBiN2F2Kw4yiLiZQD+FcWyXYAEQfvICddi+AnEO2ycIOISw7UAVxieD/Cyz5mRMohfRSwoqoz+xNuIB+cj9loEB3Pw2448NaitKSLLRck2q5pOI9O9g/t/t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sWNj8ODBcfPNNzfuJVZauckIECBAgECXQNsE4Su7UZxCcdhhh23ZVDxU6kKAAAECBPpCoG0eGu2L4sxJgAABAgTKBARhmZBxAgQIEEhaQBAm3V7FESBAgECZgCAsEzJOgAABAkkLCMKk26s4AgQIECgTEIRlQsYJECBAIGkBQZh0exVHgAABAmUCgrBMyDgBAgQIJC0gCJNur+IIECBAoExAEJYJGSdAgACBpAUEYdLtVRwBAgQIlAkIwjIh4wQIECCQtIAgTLq9iiNAgACBMgFBWCZknAABAgSSFhCESbdXcQQIECBQJiAIy4SMEyBAgEDSAoIw6fYqjgABA
</div>
<div id="color-side-bar-dendrogram-plot" class="section level3">
<h3>Color side bar dendrogram plot</h3>
<p>Asked (<a href="https://stackoverflow.com/questions/34539746/color-side-bar-dendrogram-plot/" class="uri">https://stackoverflow.com/questions/34539746/color-side-bar-dendrogram-plot/</a>)[here].</p>
<p>Solution: use the <code>color_branches</code> function.</p>
<p>Example using <code>mtcars</code>:</p>
<div class="sourceCode" id="cb36"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb36-1"><a href="#cb36-1" tabindex="-1"></a><span class="do">## mtcars example</span></span>
<span id="cb36-2"><a href="#cb36-2" tabindex="-1"></a></span>
<span id="cb36-3"><a href="#cb36-3" tabindex="-1"></a><span class="co"># Create the dend:</span></span>
<span id="cb36-4"><a href="#cb36-4" tabindex="-1"></a>dend <span class="ot">&lt;-</span> <span class="fu">as.dendrogram</span>(<span class="fu">hclust</span>(<span class="fu">dist</span>(mtcars)))</span>
<span id="cb36-5"><a href="#cb36-5" tabindex="-1"></a></span>
<span id="cb36-6"><a href="#cb36-6" tabindex="-1"></a><span class="co"># Create a vector giving a color for each car to which company it belongs to</span></span>
<span id="cb36-7"><a href="#cb36-7" tabindex="-1"></a>car_type <span class="ot">&lt;-</span> <span class="fu">rep</span>(<span class="st">&quot;Other&quot;</span>, <span class="fu">length</span>(<span class="fu">rownames</span>(mtcars)))</span>
<span id="cb36-8"><a href="#cb36-8" tabindex="-1"></a>is_x <span class="ot">&lt;-</span> <span class="fu">grepl</span>(<span class="st">&quot;Merc&quot;</span>, <span class="fu">rownames</span>(mtcars))</span>
<span id="cb36-9"><a href="#cb36-9" tabindex="-1"></a>car_type[is_x] <span class="ot">&lt;-</span> <span class="st">&quot;Mercedes&quot;</span></span>
<span id="cb36-10"><a href="#cb36-10" tabindex="-1"></a>is_x <span class="ot">&lt;-</span> <span class="fu">grepl</span>(<span class="st">&quot;Mazda&quot;</span>, <span class="fu">rownames</span>(mtcars))</span>
<span id="cb36-11"><a href="#cb36-11" tabindex="-1"></a>car_type[is_x] <span class="ot">&lt;-</span> <span class="st">&quot;Mazda&quot;</span></span>
<span id="cb36-12"><a href="#cb36-12" tabindex="-1"></a>is_x <span class="ot">&lt;-</span> <span class="fu">grepl</span>(<span class="st">&quot;Toyota&quot;</span>, <span class="fu">rownames</span>(mtcars))</span>
<span id="cb36-13"><a href="#cb36-13" tabindex="-1"></a>car_type[is_x] <span class="ot">&lt;-</span> <span class="st">&quot;Toyota&quot;</span></span>
<span id="cb36-14"><a href="#cb36-14" tabindex="-1"></a>car_type <span class="ot">&lt;-</span> <span class="fu">factor</span>(car_type)</span>
<span id="cb36-15"><a href="#cb36-15" tabindex="-1"></a>n_car_types <span class="ot">&lt;-</span> <span class="fu">length</span>(<span class="fu">unique</span>(car_type))</span>
<span id="cb36-16"><a href="#cb36-16" tabindex="-1"></a>cols_4 <span class="ot">&lt;-</span> colorspace<span class="sc">::</span><span class="fu">rainbow_hcl</span>(n_car_types, <span class="at">c =</span> <span class="dv">70</span>, <span class="at">l =</span> <span class="dv">50</span>)</span>
<span id="cb36-17"><a href="#cb36-17" tabindex="-1"></a>col_car_type <span class="ot">&lt;-</span> cols_4[car_type]</span>
<span id="cb36-18"><a href="#cb36-18" tabindex="-1"></a></span>
<span id="cb36-19"><a href="#cb36-19" tabindex="-1"></a><span class="co"># extra: showing the various clusters cuts </span></span>
<span id="cb36-20"><a href="#cb36-20" tabindex="-1"></a>k234 <span class="ot">&lt;-</span> <span class="fu">cutree</span>(dend, <span class="at">k =</span> <span class="dv">2</span><span class="sc">:</span><span class="dv">4</span>)</span>
<span id="cb36-21"><a href="#cb36-21" tabindex="-1"></a></span>
<span id="cb36-22"><a href="#cb36-22" tabindex="-1"></a><span class="co"># color labels by car company:</span></span>
<span id="cb36-23"><a href="#cb36-23" tabindex="-1"></a><span class="fu">labels_colors</span>(dend) <span class="ot">&lt;-</span> col_car_type[<span class="fu">order.dendrogram</span>(dend)]</span>
<span id="cb36-24"><a href="#cb36-24" tabindex="-1"></a><span class="co"># color branches based on cutting the tree into 4 clusters:</span></span>
<span id="cb36-25"><a href="#cb36-25" tabindex="-1"></a>dend <span class="ot">&lt;-</span> <span class="fu">color_branches</span>(dend, <span class="at">k =</span> <span class="dv">4</span>)</span>
<span id="cb36-26"><a href="#cb36-26" tabindex="-1"></a></span>
<span id="cb36-27"><a href="#cb36-27" tabindex="-1"></a><span class="do">### plots</span></span>
<span id="cb36-28"><a href="#cb36-28" tabindex="-1"></a><span class="fu">par</span>(<span class="at">mar =</span> <span class="fu">c</span>(<span class="dv">12</span>,<span class="dv">4</span>,<span class="dv">1</span>,<span class="dv">1</span>))</span>
<span id="cb36-29"><a href="#cb36-29" tabindex="-1"></a><span class="fu">plot</span>(dend)</span>
<span id="cb36-30"><a href="#cb36-30" tabindex="-1"></a><span class="fu">colored_bars</span>(<span class="fu">cbind</span>(k234[,<span class="dv">3</span><span class="sc">:</span><span class="dv">1</span>], col_car_type), dend, <span class="at">rowLabels =</span> <span class="fu">c</span>(<span class="fu">paste0</span>(<span class="st">&quot;k = &quot;</span>, <span class="dv">4</span><span class="sc">:</span><span class="dv">2</span>), <span class="st">&quot;Car Type&quot;</span>))</span></code></pre></div>
<p><img role="img" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcIAAAHCCAYAAAB8GMlFAAAEDmlDQ1BrQ0dDb2xvclNwYWNlR2VuZXJpY1JHQgAAOI2NVV1oHF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nlCwOVGhHlyldKoEBACQkAICAEbBEQQ2gAjq4WAEBACQqBoEBBBWDTus1ylEBACQkAI2CAggtAGGFktBISAEBACRYOACMKicZ/lKoWAEBACQsAGARGENsDIaiEgBISAECgaBEQQFo37LFcpBISAEBACNgiIILQBRlYLASEgBIRA0SAggrBo3Ge5SiEgBISAELBBQAShDTCyWggIASEgBIoGARGEReM+y1UKASEgBISADQIiCG2AkdVCQAgIASFQNAiIICwa91muUggIASEgBGwQEEFoA4ysFgJCQAgIgaJBQARh0bjPcpVCQAgIASFgg4AIQhtgZLUQEAJCQAgUDQIiCIvGfZarFAJCQAgIARsERBDaACOrhYAQEAJCoGgQEEFYNO6zXKUQEAJCQAjYICCC0AYYWS0EhIAQEAJFg4AIwqJxn+UqhYAQEAJCwAYBEYQ2wMhqISAEhIAQKBoERBAWjfssVykEhIAQEAI2CIggtAFGVgsBISAEhEDRICCCsGjcZ7lKISAEhIAQsEFABKENMLJaCAgBISAEigYBEYRF4z7LVQoBISAEhIANAiIIbYCR1UJACAgBIVA0CIggLBr3Wa5SCAgBISAEbBAQQWgDjKwWAkJACAiBokFABGHRuM9ylUJACAgBIWCDgAhCG
<div class="sourceCode" id="cb37"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb37-1"><a href="#cb37-1" tabindex="-1"></a><span class="co"># horiz version:</span></span>
<span id="cb37-2"><a href="#cb37-2" tabindex="-1"></a><span class="co"># dend &lt;- sort(dend)</span></span>
<span id="cb37-3"><a href="#cb37-3" tabindex="-1"></a><span class="fu">par</span>(<span class="at">mar =</span> <span class="fu">c</span>(<span class="dv">4</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">12</span>))</span>
<span id="cb37-4"><a href="#cb37-4" tabindex="-1"></a><span class="fu">plot</span>(dend, <span class="at">horiz =</span> <span class="cn">TRUE</span>)</span>
<span id="cb37-5"><a href="#cb37-5" tabindex="-1"></a><span class="fu">colored_bars</span>(<span class="fu">cbind</span>(k234[,<span class="dv">3</span><span class="sc">:</span><span class="dv">1</span>], col_car_type), dend, <span class="at">rowLabels =</span> <span class="fu">c</span>(<span class="fu">paste0</span>(<span class="st">&quot;k = &quot;</span>, <span class="dv">4</span><span class="sc">:</span><span class="dv">2</span>), <span class="st">&quot;Car Type&quot;</span>), <span class="at">horiz =</span> <span class="cn">TRUE</span>)</span>
<span id="cb37-6"><a href="#cb37-6" tabindex="-1"></a><span class="fu">legend</span>(<span class="st">&quot;topleft&quot;</span>, <span class="at">legend =</span> <span class="fu">levels</span>(car_type), <span class="at">fill =</span> cols_4)</span></code></pre></div>
<p><img role="img" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcIAAAHCCAYAAAB8GMlFAAAEDmlDQ1BrQ0dDb2xvclNwYWNlR2VuZXJpY1JHQgAAOI2NVV1oHFUUPpu5syskzoPUpqaSDv41lLRsUtGE2uj+ZbNt3CyTbLRBkMns3Z1pJjPj/KRpKT4UQRDBqOCT4P9bwSchaqvtiy2itFCiBIMo+ND6R6HSFwnruTOzu5O4a73L3PnmnO9+595z7t4LkLgsW5beJQIsGq4t5dPis8fmxMQ6dMF90A190C0rjpUqlSYBG+PCv9rt7yDG3tf2t/f/Z+uuUEcBiN2F2Kw4yiLiZQD+FcWyXYAEQfvICddi+AnEO2ycIOISw7UAVxieD/Cyz5mRMohfRSwoqoz+xNuIB+cj9loEB3Pw2448NaitKSLLRck2q5pOI9O9g/t/tkXda8Tbg0+PszB9FN8DuPaXKnKW4YcQn1Xk3HSIry5ps8UQ/2W5aQnxIwBdu7yFcgrxPsRjVXu8HOh0qao30cArp9SZZxDfg3h1wTzKxu5E/LUxX5wKdX5SnAzmDx4A4OIqLbB69yMesE1pKojLjVdoNsfyiPi45hZmAn3uLWdpOtfQOaVmikEs7ovj8hFWpz7EV6mel0L9Xy23FMYlPYZenAx0yDB1/PX6dledmQjikjkXCxqMJS9WtfFCyH9XtSekEF+2dH+P4tzITduTygGfv58a5VCTH5PtXD7EFZiNyUDBhHnsFTBgE0SQIA9p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a2dnhnN89rA27o/dzMC4Ahc11L9hm5SWweXTUqFFC62NPUk3h9cC33
</div>
<div id="plot-a-mirror-labels-on-the-left-horizontal-dendrogram" class="section level3">
<h3>Plot a “mirror” (labels on the left) horizontal dendrogram</h3>
<p>Asked (<a href="https://github.com/talgalili/dendextend/issues/29" class="uri">https://github.com/talgalili/dendextend/issues/29</a>)[here].</p>
<p>Solution: use the <code>plot_horiz.dendrogram</code> function.</p>
<p>Example using <code>USArrests</code>:</p>
<div class="sourceCode" id="cb38"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb38-1"><a href="#cb38-1" tabindex="-1"></a><span class="co"># Create the dend:</span></span>
<span id="cb38-2"><a href="#cb38-2" tabindex="-1"></a>hc <span class="ot">&lt;-</span> <span class="fu">hclust</span>(<span class="fu">dist</span>(USArrests), <span class="st">&quot;ave&quot;</span>)</span>
<span id="cb38-3"><a href="#cb38-3" tabindex="-1"></a>d <span class="ot">&lt;-</span> <span class="fu">as.dendrogram</span>(hc)</span>
<span id="cb38-4"><a href="#cb38-4" tabindex="-1"></a><span class="fu">library</span>(dendextend)</span>
<span id="cb38-5"><a href="#cb38-5" tabindex="-1"></a>d <span class="ot">&lt;-</span> d <span class="sc">%&gt;%</span> <span class="fu">color_branches</span>(<span class="at">k=</span><span class="dv">3</span>) <span class="sc">%&gt;%</span> color_labels</span>
<span id="cb38-6"><a href="#cb38-6" tabindex="-1"></a></span>
<span id="cb38-7"><a href="#cb38-7" tabindex="-1"></a><span class="co"># horiz normal version</span></span>
<span id="cb38-8"><a href="#cb38-8" tabindex="-1"></a><span class="fu">par</span>(<span class="at">mar =</span> <span class="fu">c</span>(<span class="dv">3</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">7</span>))</span>
<span id="cb38-9"><a href="#cb38-9" tabindex="-1"></a><span class="fu">plot</span>(d, <span class="at">horiz =</span> <span class="cn">TRUE</span>)</span></code></pre></div>
<p><img role="img" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgA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ndeVVCD4jdtJ07QCdHUUSY/a/i70iLgqSYPM336sXkt1vGIYPO+4XPUrT8hC2o//wJdMnJXpeWo9NqNClBZZTRqiVri92w2T1ClrvSXHEklTPtMk6nr1WNWOMYp87Au0hJH/k9dqh+3UbyHCToVTBhMEBIGegoCtmQHC3M3x4CS3816SHpFS4Cu34ihpQHYTw+r7V6bmkdZTg+jnF9dfqy2thEWIJ/TNjJXGVrA7CqWRyXCcMQTpP29B2YlU5G05CqdZw+v7ND8Iev02ZZLUXmeta//sF9Qpm0CZ5CIe/xJl0an1DjRahx49UyMNCVJrAytzZSZlEmThezUZZeqYP2xGasy2fGw9LAB5/xxR2p7/U9ci8slvoKMDZRJ2v3UqN1Fictp8yidsAmZibklMfTXkeDaMWurXGdeECDsDRRlDEBAEBIFmCFgN9FNaGJv8tN6bemZGqlW/zx6ivTobdcwaEJsOWeyJNPN3HFfkF/TabahIzEb+tmPKAYe1tvZIHmmT0S/9gD7PXA//J68B2USxa8IT6pvH0zFoSgM6pBW2JtVkotUK7/EZOlirU6fZI+Eyf6yaJ+8lHrntHYzc/Ka6p6PXsharHUf7rXNa2z0XRtr2nfndFIHOHFnGEgQEAUHgEiLAe3mjFh6Cv6NJu2aRXVyNipqmYRDnGqgiOQ9Zv++H24KJqqn3Q7OVibQiKVudGznZkMblhowlW+HzyFx1LZxIg7U9Nh+yg03S/9YoRxNTX2dYjwjCiWe/g2WYL2lnxud6fIv3K1NzYOrtTI42g9R9dlxh0ZKzOmnjR
<div class="sourceCode" id="cb39"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb39-1"><a href="#cb39-1" tabindex="-1"></a><span class="co"># horiz mirror version</span></span>
<span id="cb39-2"><a href="#cb39-2" tabindex="-1"></a><span class="fu">par</span>(<span class="at">mar =</span> <span class="fu">c</span>(<span class="dv">3</span>,<span class="dv">7</span>,<span class="dv">1</span>,<span class="dv">1</span>))</span>
<span id="cb39-3"><a href="#cb39-3" tabindex="-1"></a><span class="fu">plot_horiz.dendrogram</span>(d, <span class="at">side =</span> <span class="cn">TRUE</span>)</span></code></pre></div>
<p><img role="img" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcIAAAHCCAYAAAB8GMlFAAAEDmlDQ1BrQ0dDb2xvclNwYWNlR2VuZXJpY1JHQgAAOI2NVV1oHFUUPpu5syskzoPUpqaSDv41lLRsUtGE2uj+ZbNt3CyTbLRBkMns3Z1pJjPj/KRpKT4UQRDBqOCT4P9bwSchaqvtiy2itFCiBIMo+ND6R6HSFwnruTOzu5O4a73L3PnmnO9+595z7t4LkLgsW5beJQIsGq4t5dPis8fmxMQ6dMF90A190C0rjpUqlSYBG+PCv9rt7yDG3tf2t/f/Z+uuUEcBiN2F2Kw4yiLiZQD+FcWyXYAEQfvICddi+AnEO2ycIOISw7UAVxieD/Cyz5mRMohfRSwoqoz+xNuIB+cj9loEB3Pw2448NaitKSLLRck2q5pOI9O9g/t/tkXda8Tbg0+PszB9FN8DuPaXKnKW4YcQn1Xk3HSIry5ps8UQ/2W5aQnxIwBdu7yFcgrxPsRjVXu8HOh0qao30cArp9SZZxDfg3h1wTzKxu5E/LUxX5wKdX5SnAzmDx4A4OIqLbB69yMesE1pKojLjVdoNsfyiPi45hZmAn3uLWdpOtfQOaVmikEs7ovj8hFWpz7EV6mel0L9Xy23FMYlPYZenAx0yDB1/PX6dledmQjikjkXCxqMJS9WtfFCyH9XtSekEF+2dH+P4tzITduTygGfv58a5VCTH5PtXD7EFZiNyUDBhHnsFTBgE0SQIA9pfFtgo6cKGuhooeilaKH41eDs38Ip+f4At1Rq/sjr6NEwQqb/I/DQqsLvaFUjvAx+eWirddAJZnAj1DFJL0mSg/gcIpPkMBkhoyCSJ8lTZIxk0TpKDjXHliJzZPO50dR5ASNSnzeLvIvod0HG/mdkmOC0z8VKnzcQ2M/Yz2vKldduXjp9bleLu0ZWn7vWc+l0JGcaai10yNrUnXLP/8Jf59ewX+c3Wgz+B34Df+vbVrc16zTMVgp9um9bxEfzPU5kPqUtV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ofxIKmwiA9S+M2mI3osuUtvVKXnI+nYj/MnEWZtdpJ6HtUmbUcGoJw1RKzze4/rpape13gpaz6ITk6j7lZPUOMYo5pGP0RlGgYuv1i7b5ru8phEbYkvbHEsrqsWlkY7wdTRtc1x2BAFBQBAQBIAzToQGREoh/12Iw6QBOUwbqsO8NrOItJ4GxD/1le5YY2UtrMK9YWhhqjS2kl2xqIxJh/Ockcj+bguq4jJRtOUwXOaN0c1pvxH64i3KJKk9zlrXvvlPq102gRo72uDYo5+gKj5Tp+FpA3rMfZ2102Bka6lMlNoDBuYmyteo3WfTqVZsRwejaOMhpe0FPn41YhZ/Dj09KJOw58KZ2mEwO2E+5QNsAmZi7kzM/TX+wu4w6mweH2sgk3JmSV2b039EF2GQu7kQYRtUZEcQEAQEAQ0CZ5wI+TI2wwKUFsYmP230poGFibqDwR/dT746O7XNGhCbDlkciTSLtx9V5Bf6wi2oSc1H8V9HVABO2Mu3qjG9/SgibTL+2W9I67sOgYuvAtlEsXPqY+qb19Iz1lxbu64es1lraRXrUk8mWq2wj4/NqSwu88fB7YpJ6j7Zl3joltcxbvMr6pyeQedarDrZ6kPvhLZ7MoxaTdFtOlgY4b7pGl+m9mA6aYT2dFxEEBAEBAFBoCMCZ9RH2PpyvvfPR1NlDWlimvQGExc7WAR7IOeHrSqYhANKou94B7k/b1PTOMCGzaD1heUw93eF7dhQikBdB+uh/jAw75uJrzazAOa+rhRoMxzGDtbIWb5dXUtLzq3v92Tb+X/sUb7A+sIyjbl24iAVILNr+j+xf/5raCishdfNM8DRs1qN82Rrtj9/Mozaj5d9Q
</div>
</div>
<!-- code folding -->
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>