#' Plot HC dendrogram
#'
#' Plot HC dendrogram given a data frame.
#' @param df A data frame on which PCA plot is based.
#' @param sample_to_color_map A data frame mapping sample names to color. Make sure the order of row of 'sample_to_color_map' is the same as order of columns in 'df'. Make sure there is column named 'color'.
#' @param method_distance A character of method for calculating distances between samples. Defaults to "euclidean". Can also be one of:"maximum", "manhattan", "canberra", "binary", "minkowski", "pearson", "kendall", "spearman". The last free are derived from fdistColPearson() function.
#' @param method_agglomeration A character of sample clustering method. Defaults to "complete" (i.e. complete linkage algorithm). Can also be one of: "ward.D", "ward.D2", "single","average","mcquitty","median","centroid". See ?hclust for more info.
#' @return A HC dendrogram
#' @export
makeHCdendrogram <- function(df, sample_to_color_map, method_distance="euclidean", method_agglomeration="complete", main="hierarchical clustering dendrogram"){
## checks
if(!("dendextend" %in% rownames(installed.packages()))){ # testing if required packaged installed
stop("dendextend package is not installed. Please install it. Exiting!")
}
if(!(ncol(sample_to_color_map)==2)){
stop("sample_to_color_map does not have two columns. Ensure it has two columns. Exiting")
}
if(!("color" %in% colnames(sample_to_color_map))) {
stop("There is no column named color. Ensure there is.Exiting")
}
library(dendextend)
if(method_distance=="pearson"|method_distance=="kendall"|method_distance=="spearman"){
distObj <- f.distCol.Pearson.2(x = df, method = method_distance)
}
else{
distObj <- dist(t(df), method = method_distance)
}
hc <- hclust(d=distObj, method = method_agglomeration)
dendo <- as.dendrogram(hc)
sample_to_color_map <- sample_to_color_map[labels(dendo),]
colVec <- as.character(sample_to_color_map$color)
dendo %>% set("labels_col", as.character(colVec)) %>%
plot(main=main)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.