######################################################### # Hierarchical Clustering Dendrogram # Import Excel Dataset ######################################################### # Clear workspace rm(list = ls()) # Load package library(readxl) ######################################################### # Import Excel File ######################################################### bio_data <- read_excel(file.choose()) head(bio_data) # View data View(bio_data) ######################################################### # Set Sample Names as Row Names ######################################################### bio_df <- as.data.frame(bio_data) rownames(bio_df) <- bio_df$Sample bio_df$Sample <- NULL ######################################################### # Standardize Data ######################################################### bio_scaled <- scale(bio_df) ######################################################### # Calculate Distance Matrix ######################################################### dist_matrix <- dist( bio_scaled, method = "euclidean" ) ######################################################### # Hierarchical Clustering ######################################################### hc <- hclust( dist_matrix, method = "complete" ) ######################################################### # Plot Dendrogram ######################################################### plot( hc, main = "Cluster Dendrogram", xlab = "", sub = "", ylab = "Height", hang = -1, cex = 1 ) ######################################################### # Draw Cluster Boxes (Optional) ######################################################### rect.hclust( hc, k = 4, border = c("red","blue","green","purple") )