R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
Heatmaps are graphical representations of data where individual values are represented as colors. They're especially useful for visualizing large matrices of data.
Here's a tutorial on how to create a heatmap in R:
The base R package provides a function called heatmap()
which can be used directly.
# Sample data mat <- matrix(rnorm(100), ncol=10) rownames(mat) <- paste("Gene", 1:10) colnames(mat) <- paste("Sample", 1:10) # Create a heatmap heatmap(mat)
The heatmap()
function offers various customization options:
heatmap(mat, main = "Heatmap Title", xlab = "Samples", ylab = "Genes", col = colorRampPalette(c("blue", "white", "red"))(25), scale = "row", margins = c(5, 10) )
pheatmap
package:A popular alternative for creating heatmaps is the pheatmap
package, which provides better aesthetics and more customization options.
install.packages("pheatmap") library(pheatmap) pheatmap(mat, main = "Heatmap Title", color = colorRampPalette(c("blue", "white", "red"))(25), scale = "row" )
ggplot2
package:For those who love ggplot2, creating a heatmap is also feasible:
install.packages("ggplot2") library(ggplot2) # Convert matrix to long format mat_melted <- as.data.frame(as.table(mat)) ggplot(data = mat_melted, aes(x=Var1, y=Var2)) + geom_tile(aes(fill = Freq), color = "white") + scale_fill_gradient2(low = "blue", high = "red", mid = "white", midpoint = 0, limit = c(-2,2)) + theme_minimal() + theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1))
heatmap()
from base R provides basic heatmap visualization.pheatmap
package offers enhanced aesthetics and easy customization for heatmaps.ggplot2
provides a more flexible platform for creating complex heatmap visualizations.With this tutorial, you should now be able to create and customize heatmaps in R using various methods!
R heatmap function example:
# Create a numeric matrix or data frame data_matrix <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, byrow = TRUE) # Create a heatmap using the heatmap() function heatmap(data_matrix, main = "Heatmap Example")
Heatmap visualization in R:
# Create a numeric matrix or data frame data_matrix <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, byrow = TRUE) # Create a heatmap using the heatmap() function heatmap(data_matrix, main = "Heatmap Visualization")
Customize heatmap in R:
# Create a numeric matrix or data frame data_matrix <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, byrow = TRUE) # Customize the heatmap using additional parameters heatmap(data_matrix, main = "Customized Heatmap", col = cm.colors(10), cexRow = 2, cexCol = 2)
Heatmap with ggplot2 in R:
# Create a numeric matrix or data frame data_matrix <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, byrow = TRUE) # Create a heatmap using ggplot2 library(ggplot2) ggplot(data = as.data.frame(data_matrix), aes(x = Var1, y = Var2, fill = value)) + geom_tile() + theme_minimal()
Add labels to heatmap in R:
# Create a numeric matrix or data frame data_matrix <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, byrow = TRUE) # Add row and column labels to the heatmap heatmap(data_matrix, main = "Heatmap with Labels", Rowv = NA, Colv = NA, labRow = c("Row1", "Row2"), labCol = c("Col1", "Col2", "Col3"))
R heatmap color scale:
# Create a numeric matrix or data frame data_matrix <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, byrow = TRUE) # Specify a custom color scale for the heatmap heatmap(data_matrix, main = "Heatmap with Custom Color Scale", col = colorRampPalette(c("blue", "white", "red"))(20))
Heatmap using pheatmap in R:
# Install and load the pheatmap package # install.packages("pheatmap") library(pheatmap) # Create a numeric matrix or data frame data_matrix <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, byrow = TRUE) # Create a heatmap using pheatmap pheatmap(data_matrix, main = "Heatmap using pheatmap")
Interactive heatmap in R:
For interactive heatmaps, you can use packages like heatmaply
or plotly
to create interactive visualizations.
Example using heatmaply
:
# Install and load the heatmaply package # install.packages("heatmaply") library(heatmaply) # Create a numeric matrix or data frame data_matrix <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, byrow = TRUE) # Create an interactive heatmap using heatmaply heatmaply(data_matrix, main = "Interactive Heatmap using heatmaply")