How to use Dendextend Library In R Language
R
# Install and load required packages install.packages ( "gplots" ) library (gplots) library (dendextend) # Sample data set.seed (123) data_matrix <- matrix ( rnorm (100), nrow = 10) rownames (data_matrix) <- paste ( "Row" , 1:10) colnames (data_matrix) <- paste ( "Col" , 1:10) # Calculate hierarchical clustering row_dendro <- as.dendrogram ( hclust ( dist (data_matrix))) col_dendro <- as.dendrogram ( hclust ( dist ( t (data_matrix)))) # Create a heatmap with dendrograms using heatmap.2 heatmap_plot <- heatmap.2 (data_matrix, dendrogram = "row" , Colv = col_dendro, trace = "none" , col = colorRampPalette ( c ( "blue" , "white" , "red" ))(50), scale = "none" , key = TRUE , keysize = 1.0, key.title = NA ) print (heatmap_plot) |
Output:
- We use the heatmap.2() function from the gplots package to create the heatmap.
- We specify dendrogram = “row” to add a row dendrogram.
- Colv = col_dendro specifies the column dendrogram.
- trace = “none” removes the trace lines.
- col specifies the color palette for the heatmap.
- scale = “none” prevents scaling of the data.
- key = TRUE adds a color key.
- keysize = 1.0 sets the size of the color key.
- key.title = NA removes the color key title.
Adding a Dendrogram to a Heatmap in R using heatmaply
R
# Load necessary libraries library (heatmaply) # Create a sample data matrix (replace with your actual data) set.seed (123) data_matrix <- matrix ( rnorm (100), nrow = 10) colnames (data_matrix) <- letters [1:10] rownames (data_matrix) <- letters [11:20] # Calculate hierarchical clustering for rows and columns hc_rows <- hclust ( dist (data_matrix)) hc_cols <- hclust ( dist ( t (data_matrix))) # Create dendrograms row_dendro <- as.dendrogram (hc_rows) col_dendro <- as.dendrogram (hc_cols) # Create the heatmap with dendrograms using heatmaply heatmaply (data_matrix, Rowv = row_dendro, Colv = col_dendro) |
Output:
- We generate a sample data matrix (data_matrix) for demonstration purposes. You should replace this with your actual data.
- We perform hierarchical clustering on both the rows and columns of the data matrix using hclust and dist functions. This creates dendrogram objects (row_dendro and col_dendro) that represent the hierarchical structure of the data.
- The main part of the code is the creation of the heatmap with dendrograms using the heatmaply function.
- The heatmaply function generates an interactive heatmap plot. You can explore this plot by clicking and interacting with it. It provides features like zooming, hovering to see data values, and more.
- Remember to replace the sample data (data_matrix) with your actual dataset to visualize your data in the heatmap with dendrograms.
Adding a Dendrogram to a ggplot2 Heatmap in R
A potent technique that improves the visualisation of hierarchical clustering patterns inside data is to add a dendrogram to a ggplot2 heatmap in R. Dendrograms shed light on the connections and resemblances among data points, assisting in the discovery of distinct clusters or groups. We will examine how to combine dendrograms with ggplot2 heatmaps using R in this article, enabling a more thorough comprehension of intricate data structures and patterns.