How to create a fast and easy heatmap with ggplot2

[This article was first published on R – DATABIOMICS, 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 heatmaps are a tool of data visualization broadly widely used with biological data. The concept is to represent a matrix of values as colors where usually is organized by a gradient. We can find a large number of these graphics in scientific articles related with gene expressions, such as microarray or RNA-seq.

In the next example, we are going to represent a dataframe of gene expression values of 20 genes and 20 patients.

# Load of libraries

library(reshape2)
library(ggplot2)

# Labels of rows and columns 

name_genes <- paste(rep("GEN", 20), LETTERS[1:20], sep="_") # rows
name_patients <- paste(rep("PATIENT", 20), seq(1,20,1), sep="_") # columns

# Generation of dataframe

value_expression <- data.frame(genes = name_genes, 
                               matrix(rnorm(400, 2, 1.8),nrow = 20, ncol = 20))
names(value_expression)[2:21] <- name_patients

# Melt dataframe

df_heatmap <- melt(value_expression, id.vars = "genes")
names(df_heatmap)[2:3] <- c("patient", "expression_level")

head(df_heatmap)

genes   patient expression_level
1 GEN_A PATIENT_1       -0.5312459
2 GEN_B PATIENT_1        3.4286519
3 GEN_C PATIENT_1       -3.4287675
4 GEN_D PATIENT_1        2.7277366
5 GEN_E PATIENT_1        4.4684617
6 GEN_F PATIENT_1        2.3539905

Once we have our dataframe (df_heatmap), we can visualize the values with the package ggplot2.

# Elaboration of heatmap (white - steelblue)

ggplot(df_heatmap, aes(patient, genes )) +
  geom_tile(aes(fill = expression_level), color = "white") +
  scale_fill_gradient(low = "white", high = "steelblue") +
  ylab("List of genes ") +
  xlab("List of patients") +
  theme(legend.title = element_text(size = 10),
        legend.text = element_text(size = 12),
        plot.title = element_text(size=16),
        axis.title=element_text(size=14,face="bold"),
        axis.text.x = element_text(angle = 90, hjust = 1)) +
  labs(fill = "Expression level")

rplot

# Elaboration of heatmap with custom gradient (red-steelblue)


ggplot(df_heatmap, aes(patient, genes )) +
  geom_tile(aes(fill = expression_level), , color = "white") +
  scale_fill_gradient(low = "red", high = "steelblue") +
  ylab("List of genes ") +
  xlab("List of patients") +
  theme(legend.title = element_text(size = 10),
        legend.text = element_text(size = 12),
        plot.title = element_text(size=16),
        axis.title=element_text(size=14,face="bold"),
        axis.text.x = element_text(angle = 90, hjust = 1)) +
  labs(fill = "Expression level")

rplot01

# Elaboration of heatmap with custom gradient (red - green)


ggplot(df_heatmap, aes(patient, genes )) +
  geom_tile(aes(fill = expression_level), , color = "white") +
  scale_fill_gradient(low = "red", high = "green") +
  ylab("List of genes ") +
  xlab("List of patients") +
  theme(legend.title = element_text(size = 10),
        legend.text = element_text(size = 12),
        plot.title = element_text(size=16),
        axis.title=element_text(size=14,face="bold"),
        axis.text.x = element_text(angle = 90, hjust = 1)) +
  labs(fill = "Expression level")

rplot09

 

To leave a comment for the author, please follow the link and comment on their blog: R – DATABIOMICS.

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)