Open In App

Create a Heatmap in R Programming - heatmap() Function

Last Updated : 17 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A heatmap() function in R Programming Language is used to plot a heatmap. A heatmap is defined as a graphical representation of data using colors to visualize the value of the matrix. It is used to represent more common values or higher activities brighter colors reddish colors are used and to less common or activity values darker colors are preferred. Heatmap is also defined by the name of the shading matrix. 

Syntax:

heatmap(data)

Parameters: 

  • data: It represent matrix data, such as values of rows and columns

1. Create a Heatmap in R Programming Language

In this example, number of rows and columns are specified to draw heatmap with a given function.

r
set.seed(110)

data <- matrix(rnorm(100, 0, 5), nrow = 10, ncol = 10)

colnames(data) <- paste0("col", 1:10)
rownames(data) <- paste0("row", 1:10)

heatmap(data)

Output: 

gh
Heatmap in R Programming

2. Create heatmap in R using colorRampPalette

In this example, heat map is drawn by using colorRampPalette to merge two different colors.

r
set.seed(110)

data <- matrix(rnorm(100, 0, 5), nrow = 10, ncol = 10)
 
colnames(data) <- paste0("col", 1:10)
rownames(data) <- paste0("row", 1:10)

my_colors <- colorRampPalette(c("cyan", "darkgreen"))

heatmap(data, col = my_colors(100))

Output: 

ghh
Heatmap in R Programming

3. Adding Title and Axis Labels to the Heatmap

In this example we give the title of the heatmap using the main argument and the xlab ,ylab arguments are used to label the x and y axes, respectively.

R
set.seed(110)

data <- matrix(rnorm(100, 0, 5), nrow = 10, ncol = 10)
   
colnames(data) <- paste0("col", 1:10)
rownames(data) <- paste0("row", 1:10)

my_colors <- colorRampPalette(c("cyan", "darkgreen"))

heatmap(data, col = my_colors(100), main = "Customized Heatmap", 
        xlab = "Columns", ylab = "Rows")

Output:

gh
Heatmap in R Programming

4. Margins Around the Heatmap Plot

In this example we plot a heatmap with specified margins around the plot using the margins argument. Here the first value (5) controls the bottom margin and the second value (10) controls the right margin.

R
set.seed(110)

data <- matrix(rnorm(100, 0, 5), nrow = 10, ncol = 10)

colnames(data) <- paste0("col", 1:10)
rownames(data) <- paste0("row", 1:10)

my_colors <- colorRampPalette(c("cyan", "darkgreen"))

heatmap(data, col = my_colors(100), main = "Customized Heatmap", 
        xlab = "Columns", ylab = "Rows", margins = c(5, 10))

Output:

gh
Heatmap in R Programming

5. Heatmap in R without Dendrogram

In this example we plot a heatmap without dendrograms by setting Colv = NA and Rowv = NA, which removes the hierarchical clustering and dendrogram from both rows and columns.

R
set.seed(110)

data <- matrix(rnorm(100, 0, 5), nrow = 10, ncol = 10)

colnames(data) <- paste0("col", 1:10)
rownames(data) <- paste0("row", 1:10)

my_colors <- colorRampPalette(c("cyan", "darkgreen"))

heatmap(data, col = my_colors(100), main = "Customized Heatmap", 
        xlab = "Columns", ylab = "Rows", margins = c(5, 10), Colv = NA, Rowv = NA)

Output:

gh
Heatmap in R Programming

Next Article

Similar Reads