How to draw heatmap in r: Quick and Easy way

[This article was first published on Data Science Tutorials, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post How to draw heatmap in r: Quick and Easy way appeared first on
Data Science Tutorials –

How to draw heatmap in r?, A heatmap is essentially a table with colors replacing the numbers. The colors represent the measuring level. It can help you locate highs and lows, as well as patterns.

In this article, We’ll show you how to create a clustered heatmap with the R programming language’s pheatmap package.

Let’s get this article started,

How to draw heatmap in r

Raivo Kolde’s pheatmap program provides great control over heatmap proportions and appearance. The package’s main benefit is that it allows users to visually cluster heatmaps.

How to interpret Margin of Error Results? »

Software Package with Example Data and Heatmaps

Let’s start by creating some sample data.

set.seed(123)                
data <- matrix(round(rnorm(150), 2), nrow = 15)
colnames(data) <- LETTERS[1:10]
rownames(data) <- letters[1:15]
data                                
    A     B     C     D     E     F     G     H     I     J
a -0.56  1.79  0.43 -1.12  0.38  1.03  0.99 -0.05  0.12  1.13
b -0.23  0.50 -0.30 -0.40 -0.50 -0.28  0.55 -0.78 -0.95 -1.46
c  1.56 -1.97  0.90 -0.47 -0.33 -1.22  0.24 -1.67 -0.49  0.74
d  0.07  0.70  0.88  0.78 -1.02  0.18 -0.63 -0.38 -0.26  1.91
e  0.13 -0.47  0.82 -0.08 -1.07 -0.14  1.36  0.92  1.84 -1.44
f  1.72 -1.07  0.69  0.25  0.30  0.01 -0.60 -0.58 -0.65  0.70
g  0.46 -0.22  0.55 -0.03  0.45  0.39  2.19  0.61  0.24 -0.26
h -1.27 -1.03 -0.06 -0.04  0.05 -0.37  1.53 -1.62  0.08 -1.57
i -0.69 -0.73 -0.31  1.37  0.92  0.64 -0.24 -0.06 -0.96 -1.51
j -0.45 -0.63 -0.38 -0.23  2.05 -0.22 -1.03  0.52 -0.07 -1.60
k  1.22 -1.69 -0.69  1.52 -0.49  0.33 -0.71  0.30  1.44 -0.53
l  0.36  0.84 -0.21 -1.55 -2.31  1.10  0.26  0.11  0.45 -1.46
m  0.40  0.15 -1.27  0.58  1.01  0.44 -0.25 -0.64  0.04  0.69
n  0.11 -1.14  2.17  0.12 -0.71 -0.33 -0.35 -0.85 -0.42  2.10
o -0.56  1.25  1.21  0.22 -0.69  1.15 -0.95 -1.02 -2.05 -1.29

Take a look at the table above. Our sample data is a matrix with 15 rows and ten numerical columns labeled “A,” “B,” “C,” “D,” “E,” “F,” “G,” “H,” “I,” and “J.”

If we wish to use the functions provided in the package, we must additionally install and load the pheatmap package into R.

install.packages("pheatmap")        
library("pheatmap")

Let’s use the pheatmap package to create some heatmaps!

Example 1: Using the pheatmap Package, create a default heatmap

The pheatmap function is used to draw a heatmap in the following code. Note that the only thing we need to mention is the name of the data matrix we wish to draw.

pheatmap(data)                      

Figure 1 shows the visual made by the previous R code – a heatmap created using the pheatmap package’s default settings.

How to make a rounded corner bar plot in R? – Data Science Tutorials

Example 2: Create a heatmap using kmeans clusters

The pheatmap function has a number of extra options that can be used to make the heatmap more appealing and show more clusters.

Example 2 explains how to partition our heatmap into a specific number of clusters using the kmeans cluster method (i.e. 4).

pheatmap(data, kmeans_k = 4)

Our data is represented in Figure 2 as a heatmap with four clusters. The cells in these clusters have been merged, as you can see.

Example 3: Create a heatmap with row clusters

We’ll teach you how to partition our heatmap without merging the cells in the distinct groups in Example 3. (as in Example 2).

As shown below, the cutree rows option in the pheatmap function can be used for this.

5 Free Books to Learn Statistics For Data Science – Data Science Tutorials

pheatmap(data, cutree_rows = 4)

Our heatmap is divided into four categories by rows in Figure 3.

Example 4: Create a heatmap with row and column clusters

This example shows how to divide a heatmap into columns and rows (not only by rows as in Example 3).

In addition to the cutree rows parameter, we must also specify the cutree cols argument.

pheatmap(data, cutree_rows = 4, cutree_cols = 3)

Our heatmap is clustered by rows and columns in Figure 4.

For interesting posts, don’t forget to subscribe to the newsletter.

Learning Tips!

The post How to draw heatmap in r: Quick and Easy way appeared first on Data Science Tutorials

To leave a comment for the author, please follow the link and comment on their blog: Data Science Tutorials.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)