Create Heatmap in R
Heatmap is a graphical way to represent data. It is most commonly used in data analysis. In data analysis, we explore the dataset and draws insight from the dataset, we try to find the hidden patterns in the data by doing a visual analysis of the data. Heatmap visualizes the value of the matrix with colours, where brighter the colour means the higher the value is, and lighter the colour means the lower the value is. In Heatmap, we use warmth to cool colour scheme. Heatmap is a so-called heatmap because in heatmap we map the colours onto the different values that we have in our dataset.
In this article, we will discuss how to create heatmaps in R Programming Language.
Method 1: Using the geom_tile() from the ggplot2 package
geom_tile() is used to create a 2-Dimensional heatmap with the plane rectangular tiles in it. It comes pre-installed with the ggplot2 package for R programming.
geom_tile( mapping =NULL, data=NULL, stat=”Identity”,…)
- mapping: It can be “aes”. “aes” is an acronym for aesthetic mapping. It describes how variables in the data are mapped to the visual properties of the geometry.
- data: It holds the dataset variable, a variable where we have stored our dataset in the script.
- stat: It is used to perform any kind of statistical operation on the dataset.
To create a heatmap, first, import the required libraries and then either create or load a dataset. Now simply call geom_tile() function with the appropriate values for parameters.
Method 2: Using heatmap() function of base R
heatmap() function comes with the default installation of the Base R. One can use the default heatmap() also if he does not want to install any extra package. We can plot heatmap of the dataset using this heatmap function from the R.
heatmap(data,main = NULL, xlab = NULL, ylab = NULL,…)
data: data specifies the matrix of the data for which we wanted to plot a Heatmap.
main: main is a string argument, and it specifies the title of the plot.
xlab: xlab is used to specify the x-axis labels.
ylab: ylab is used to specify the y-axis labels.
Here the task is straightforward. You only need to put in the values that the function heatmap() requires.