Frequently asked questions

Tal Galili



Questions are often taken from here the stackoverflow dendrogram tag.

How to colour the labels of a dendrogram by an additional factor variable

Asked ([here].

Solution: use the labels_colors function.

##   Arkansas    Arizona California    Alabama     Alaska 
##          1          2          3          4          5

Instead of using 1:5, 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:

## [1] 1 2 3 1 2 3
## [1] 1 1 2 2 3 3
##   1   2  51  52 101 102 
##   1   1   2   2   3   3

How to color a dendrogram’s labels according to defined groups? (in R)

Asked ([here].

Solution: use the color_labels function.

I suspect the function you are looking for is either color_labels or get_leaves_branches_col. The first color your labels based on cutree (like color_branches 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 branches_attr_by_labels). For example:

Either way, you should always have a look at the set function, for ideas on what can be done to your dendrogram (this saves the hassle of remembering all the different functions names).

How to color a dendrogram’s branches/labels based on cluster (i.e.: cutree result)

Use the color_branches and color_labels functions, with the k (orh) parameter:

Change dendrogram’s labels

Use the left assign labels<- function:

## [1] "Arkansas"   "Arizona"    "California" "Alabama"    "Alaska"
## [1] 1 2 3 4 5

Larger font for leaves in a dendrogram

Asked ([here].

Solution: use the set function, with the “labels_cex” parameter.

## [1] NA
## $lab.cex
## [1] 2
## $pch
## [1] NA

(Note that changing the spacing between the labels is currently not implemented)

How to view attributes of a dendrogram

Asked ([here], and ([here].

It generally depends on which attribute we want to view. For “midpoint” (or height) use the get_nodes_attr function, with the “midpoint” parameter.

## [1] 1.25   NA 1.50 0.50   NA   NA 0.50   NA   NA
## [1] 108.85192   0.00000  63.00833  23.19418   0.00000   0.00000  37.17701
## [8]   0.00000   0.00000

To also change an attribute, you can use the various assign functions from the package: assign_values_to_leaves_nodePar, assign_values_to_leaves_edgePar, assign_values_to_nodes_nodePar, assign_values_to_branches_edgePar, remove_branches_edgePar, remove_nodes_nodePar

How to color the branches in heatmap.2?

Asked ([here].

Solution: use the color_branches function (or the set function, with the “branches_k_color”, “k”, and “value” parameters).

(Getting the data for this example is from the ([original SO question])

test <- test0
rnames <- test[,1] 
test <- data.matrix(test[,2:ncol(test)]) # to matrix
rownames(test) <- rnames                 
test <- scale(test, center=T, scale=T) # data standarization
test <- t(test) # transpose

## Creating a color palette & color breaks

my_palette <- colorRampPalette(c("forestgreen", "yellow", "red"))(n = 299)

col_breaks = c(seq(-1,-0.5,length=100),  # forestgreen
               seq(-0.5,0.5,length=100), # yellow
               seq(0.5,1,length=100))    # red

# distance & hierarchical clustering
distance = dist(test, method ="euclidean")    
hcluster = hclust(distance, method ="ward.D")

dend1 <- as.dendrogram(hcluster)

# Get the dendextend package
if(!require(dendextend)) install.packages("dendextend")
# get some colors
cols_branches <- c("darkred", "forestgreen", "orange", "blue")
# Set the colors of 4 branches
dend1 <- color_branches(dend1, k = 4, col = cols_branches)
# or with:
# dend1 <- set(dend1, "branches_k_color", k = 4, value = cols_branches)

# Get the colors of the tips of the dendrogram:
# col_labels <- cols_branches[cutree(dend1, k = 4)] # this may need tweaking in various cases - the following is a more general solution.

col_labels <- get_leaves_branches_col(dend1)
# But due to the way heatmap.2 works - we need to fix it to be in the 
# order of the data!   
col_labels <- col_labels[order(order.dendrogram(dend1))]

# plot(dend1)
# a <- heights_per_k.dendrogram(dend1)
# library(dendextendRcpp)
# a2 <- heights_per_k.dendrogram(dend1)
# nleaves(dend1)

# Creating Heat Map
# if(!require(gplots)) install.packages("gplots")
    main = paste( "test"),  
        margins =c(5,7),      
        Rowv = dend1,  
        Colv = "NA", 
        key.xlab = "Concentration (index)",
        cexRow =0.6,
        cexCol = 0.8,
        na.rm = TRUE,
        RowSideColors = col_labels # to add nice colored strips     
      # colRow = col_labels # to add nice colored labels - only for qplots 2.17.0 and higher

For package developers - how to call imported calls from dendextend 0.18.3?

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:

Before using any of its function (for example: dendextend::color_branches ). As of dendextend version 1.0.0, this is no longer required.

How to plot a fan (Polar) Dendrogram in R?

Asked ([here].

Solution: use the circlize_dendrogram function.

A way to calculate lowest value of h in cut that produces groupings of a given minimum size?

Asked ([here].

Solution: use the heights_per_k.dendrogram function.

This feature is available in the dendextend package with the heights_per_k.dendrogram function (which also has a faster C++ implementation when loading the dendextendRcpp function).

##        1        2        3        4 
## 86.47086 68.84745 45.98871 28.36531

As a sidenote, the dendextend package has a cutree.dendrogram S3 method for dendrograms (which works very similarly to cutree for hclust objects).

Coloring dendrogram’s end branches (or leaves) based on column number of data frame in R

Asked ([here].

Solution: use the assign_values_to_leaves_edgePar function.

Color side bar dendrogram plot

Asked ([here].

Solution: use the color_branches function.

Example using mtcars:

Plot a “mirror” (labels on the left) horizontal dendrogram

Asked ([here].

Solution: use the plot_horiz.dendrogram function.

Example using USArrests: