# 7+ ways to plot dendrograms in R

##### Posted on October 03, 2012

Today we are going to talk about the wide spectrum of functions and methods that we can use to visualize dendrograms in R. You can check an extended version of this post with the complete reproducible code in R in this Rpub.

A quick reminder: a dendrogram (from Greek dendron=tree, and gramma=drawing) is nothing more than a tree diagram that practitioners use to depict the arrangement of the clusters produced by hierarchical clustering.

### 1) Basic dendrograms

Let’s start with the most basic type of dendrogram. For that purpose we’ll use the mtcars dataset and we’ll calculate a hierarchical clustering with the function hclust() (with the default options).

### 2) A less basic dendrogram

In order to add more format to the dendrograms like the one above, we simply need to tweek the right parameters.

An alternative way to produce dendrograms is to specifically convert "hclust" objects into "dendrograms" objects.

### 3) Zooming-in on dendrograms

Another very useful option is the ability to inspect selected parts of a given tree. For instance, if we wanted to examine the top partitions of the dendrogram, we could cut it at a height of 75

### 4) More customizable dendrograms

In order to get more customized graphics we need a little bit of more code. A very useful resource is the function dendrapply() that can be used to apply a function to all nodes of a dendrgoram. This comes very handy if we want to add some color to the labels.

### 5) Phylogenetic trees

Closely related to dendrograms, phylogenetic trees are another option to display tree diagrams showing the relationships among observations based upon their similarities.

A very nice tool for displaying more appealing trees is provided by the R package "ape". In this case, what we need is to convert the "hclust" objects into "phylo" objects with the funtions as.phylo(). The plot.phylo() function has four more different types for plotting a dendrogram. Here they are:

What I really like about the ape package is that we have more control on the appearance of the dendrograms, being able to customize them in different ways. For example, we can tweek some parameters according to our needs

### 6) Dendrograms with ggdendro

For reasons that are unknown to me, the The R package "ggplot2" have no functions to plot dendrograms. However, the ad-hoc package "ggdendro" offers a decent solution. You would expect to have more customization options, but so far they are rather limited. Anyway, for those of us who are ggploters this is another tool in our toolkit.

### 7) Colored dendrogram

Last but not least, there’s one more resource available from Romain Francois’s addicted to R gallery which I find really interesting. The code in R for generating colored dendrograms, which you can download and modify if wanted so, is available here

For a more detailed version of the code presented in this post, check this Rpub.

Published in categories how-to  Tagged with cluster  multivariate  dendrogram  plot  R