Link to project files on GitHUB
Link to projects on RPubs

Introduction

Here I’m going to show some basic heatmaps.

Simple heatmap 1


library(gplots)
library(RColorBrewer)

# 1. Reading in data and transform it to matrix format

data <- read.csv("dataset.csv", comment.char="#")
rnames <- data[,1]                            # assign labels in column 1 to "rnames"
mat_data <- data.matrix(data[,2:ncol(data)])  # transform column 2-5 into a matrix
rownames(mat_data) <- rnames                  # assign row names 

# 2. Customizing and plotting heatmap

# 2.1 Creates a own color palette from red to green    

my_palette <- colorRampPalette(c("white", "#FCF14C", "red"))(n = 799)   

# 2.2 Defining the color breaks manually for a "skewed" color transition

breaks <- c(seq(-1, 0,length=300),seq(0.001,0.5,length=300),seq(0.501,1,length=200))

# 2.3 Creating heatmap

heatmap.2(mat_data, 
  cellnote = mat_data,  # same data set for cell labels
  main = "Correlation", # heat map title
  notecol="black",      # change font color of cell labels to black
  density.info="none",  # turns off density plot inside color legend
  trace="none",         # turns off trace lines inside the heat map
  margins =c(12,9),     # widens margins around plot
  col=my_palette,       # use on color palette defined earlier 
  breaks=breaks,    # enable color transition at specified limits
  dendrogram="row",     # only draw a row dendrogram
  Colv="NA")            # turn off column clustering)


dev.off()
## null device 
##           1

Let’s make the same graph by d3heatmap package:

library(d3heatmap)
d3heatmap(mat_data, scale="column", color = scales::col_quantile("Reds", NULL, 5))

Heatmap with clustering

# 1. Reading in data and transform it to matrix format

data <- read.csv("dataset.csv", comment.char="#")
rnames <- data[,1]                            # assign labels in column 1 to "rnames"
mat_data <- data.matrix(data[,2:ncol(data)])  # transform column 2-5 into a matrix
rownames(mat_data) <- rnames                  # assign row names 

# 2. Customizing and plotting heatmap

# 2.1 Creates a own color palette from red to green    

my_palette <- colorRampPalette(c("white", "#FCF14C", "red"))(n = 799)   

# 2.2 Defining the color breaks manually for a "skewed" color transition

breaks <- c(seq(-1, 0,length=300),seq(0.001,0.5,length=300),seq(0.501,1,length=200))


# 3. Changes the distance measure and clustering method

# NOTE: Matrix here not symmetrical. For symmetrical matrices
# only one distance and cluster could and SHOULD be defined.
# Distance options: euclidean (default), maximum, canberra, binary, minkowski, manhattan
# Cluster options: complete (default), single, average, mcquitty, median, centroid, ward

row_distance = dist(mat_data, method = "manhattan")
row_cluster = hclust(row_distance, method = "ward")
col_distance = dist(t(mat_data), method = "manhattan")
col_cluster = hclust(col_distance, method = "ward")

heatmap.2(mat_data, 
  cellnote = mat_data,  # same data set for cell labels
  main = "Correlation", # heat map title
  notecol = "black",      # change font color of cell labels to black#
  density.info = "none",  # turns off density plot inside color legend
  trace = "none",         # turns off trace lines inside the heat map
  margins = c(12,9),     # widens margins around plot
  col = my_palette,       # use on color palette defined earlier 
  breaks = breaks,    # enable color transition at specified limits
  Rowv = as.dendrogram(row_cluster), # apply default clustering method
  Colv = as.dendrogram(col_cluster)) # apply default clustering method

dev.off()
## null device 
##           1

Heatmap with clustering and categorazing


library(gplots)
library(RColorBrewer)

# 1. Reading in data and transform it to matrix format

data <- read.csv("dataset.csv", comment.char="#")
rnames <- data[,1]                            # assign labels in column 1 to "rnames"
mat_data <- data.matrix(data[,2:ncol(data)])  # transform column 2-5 into a matrix
rownames(mat_data) <- rnames                  # assign row names 

# 2. Customizing and plotting heatmap

# 2.1 Creates a own color palette from red to green    

my_palette <- colorRampPalette(c("white", "#FCF14C", "red"))(n = 799)   

# 2.2 Defining the color breaks manually for a "skewed" color transition

breaks <- c(seq(-1, 0,length=300),seq(0.001,0.5,length=300),seq(0.501,1,length=200))

# 3. Changes the distance measure and clustering method

# NOTE: Matrix here not symmetrical. For symmetrical matrices
# only one distance and cluster could and SHOULD be defined.
# Distance options: euclidean (default), maximum, canberra, binary, minkowski, manhattan
# Cluster options: complete (default), single, average, mcquitty, median, centroid, ward

row_distance = dist(mat_data, method = "manhattan")
row_cluster = hclust(row_distance, method = "ward")
col_distance = dist(t(mat_data), method = "manhattan")
col_cluster = hclust(col_distance, method = "ward")

heatmap.2(mat_data, 
  cellnote = mat_data,  # same data set for cell labels
  RowSideColors = c(    # grouping row-variables into different
     rep("blue", 3),   # categories, Measurement 1-3: green
     rep("black", 5),    # Measurement 4-8: blue
     rep("green", 2)),    # Measurement 9-10: red
  main = "Categories",  # heat map title
  notecol="black",      # change font color of cell labels to black
  density.info="none",  # turns off density plot inside color legend
  trace="none",         # turns off trace lines inside the heat map
  margins =c(16,16),     # widens margins around plot
  col=my_palette,       # use on color palette defined earlier 
  breaks=breaks,    # enable color transition at specified limits
  Rowv = as.dendrogram(row_cluster), # apply default clustering method
  Colv = as.dendrogram(col_cluster)) # apply default clustering method

# adding a color legend for the categories
par(lend = 1)           # square line ends  for the color legend
legend("topright",      # location of the legend on the heatmap plot
    legend = c("A", "B", "C"), # category labels
    col = c("blue", "black", "green"),  # color key
    lty = 1,            # line style
    lwd = 10,           # line width
   )          


dev.off()
## null device 
##           1