Create Heatmap in R - GeeksforGeeks

29 Dec, 2021
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.
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.
Syntax:
geom_tile( mapping =NULL, data=NULL, stat=”Identity”,...)
Parameter:
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.
Example:
R
# Plotting Heatmap in R  # adding ggplot2 library for plottinglibrary(ggplot2)  # Creating random dataset# with 20 alphabets and 20 animal names letters <- LETTERS[1:20]animal_names <- c("Canidae","Felidae","Cat","Cattle",                  "Dog","Donkey","Goat","Guinea pig",                  "Horse","Pig","Rabbit","Badger",                  "Bald eagle","Bandicoot","Barnacle",                  "Bass","Bat","Bear","Beaver","Bedbug",                  "Bee","Beetle")  data <- expand.grid(X=letters, Y=animal_names)data$count <- runif(440, 0, 6)  # plotting the heatmapplt <- ggplot(data,aes( X, Y,fill=count))plt <- plt + geom_tile()  # further customizing the heatmap by# applying colors and titleplt <- plt + theme_minimal()  # setting gradient color as red and whiteplt <- plt + scale_fill_gradient(low="white", high="red")  # setting the title and subtitles using# title and subtitleplt <- plt + labs(title = "Heatmap")plt <- plt + labs(subtitle = "A simple heatmap using geom_tile()")  # setting x and y labels using labsplt <- plt + labs(x ="Alphabets", y ="Random column names")  # plotting the Heatmapplt
 
 
R
# Plotting Heatmap in R  # adding ggplot2 library for plottinglibrary(ggplot2)  # Creating random dataset# with 20 alphabets and 20 animal names letters <- LETTERS[1:20]animal_names <- c("Canidae","Felidae","Cat","Cattle",                  "Dog","Donkey","Goat","Guinea pig",                  "Horse","Pig","Rabbit","Badger",                  "Bald eagle","Bandicoot","Barnacle",                  "Bass","Bat","Bear","Beaver","Bedbug",                  "Bee","Beetle")  data <- expand.grid(X=letters, Y=animal_names)data$count <- runif(440, 0, 6)  # plotting the heatmapplt <- ggplot(data,aes( X, Y,fill=count))plt <- plt + geom_tile()  # further customizing the heatmap by# applying colors and titleplt <- plt + theme_minimal()  # setting gradient color as red and whiteplt <- plt + scale_fill_gradient(low="white", high="red")  # setting the title and subtitles using# title and subtitleplt <- plt + labs(title = "Heatmap")plt <- plt + labs(subtitle = "A simple heatmap using geom_tile()")  # setting x and y labels using labsplt <- plt + labs(x ="Alphabets", y ="Random column names")  # plotting the Heatmapplt
Output:
 
Heatmap using ggplot2
 
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. 
 
Syntax:
heatmap(data,main = NULL, xlab = NULL, ylab = NULL,...)
Parameter:
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.
 
Example:
 
R
# Heatplot from Base R# using default mtcars dataset from the Rx  <- as.matrix(mtcars)  # custom colorsnew_colors <- colorRampPalette(c("cyan", "darkgreen"))   # plotting the heatmapplt <- heatmap(x,               # assigning new colors               col = new_colors(100),                                # adding title               main = "Heatmap for mtcars dataset",                                # adding some margin so that                # it doesn not drawn over the                # y-axis label               margins = c(5,10),                                # adding x-axis labels               xlab = "variables",                                # adding y-axis labels               ylab = "Car Models",                                # to scaled the values into                # column direction               scale = "column")
 
 
Output:
 
Heatmap
 
rajeev0719singh
simranarora5sos
arorakashish0911
Picked
R-Charts
R-ggplot
R-Graphs
R-plots
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
How to Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R
How to import an Excel File into R ?
How to filter R DataFrame by values in a column?
Time Series Analysis in R
Logistic Regression in R Programming
R - if statement