Introduction to Biostatistical Computing PHC 6937

R graphics heatmap.2

Zhiguang Huo (Caleb)

Monday September 19, 2022

Heatmap.2

library(gplots) ## heatmap.2 is available in gplots
## 
## Attaching package: 'gplots'
## The following object is masked from 'package:stats':
## 
##     lowess
?heatmap.2

Application on Iris data

dataMatrix <- as.matrix(iris[,1:4])
heatmap.2(dataMatrix)

dataMatrix <- as.matrix(iris[,1:4])
heatmap.2(dataMatrix, trace = "none")

dataMatrix2 <- as.matrix(iris[1:5,1:4])
heatmap.2(dataMatrix2, trace = "none", 
          cellnote = dataMatrix2, notecol = "black")

dataMatrix <- as.matrix(iris[,1:4])
species <- iris$Species
color0 <- species
levels(color0) <- palette()[1:length(levels(species))]
color <- as.character(color0)

heatmap.2(dataMatrix, trace = "none", RowSideColors = color)
legend("topright", legend=levels(species),fill=levels(color0))

dataMatrix <- as.matrix(iris[,1:4])
species <- iris$Species
color0 <- species
levels(color0) <- palette()[1:length(levels(species))]
color <- as.character(color0)

heatmap.2(dataMatrix, trace = "none", RowSideColors = color,
           Colv=NA, Rowv = NA )
## Warning in heatmap.2(dataMatrix, trace = "none", RowSideColors = color, :
## Discrepancy: Rowv is FALSE, while dendrogram is `both'. Omitting row dendogram.
## Warning in heatmap.2(dataMatrix, trace = "none", RowSideColors = color, :
## Discrepancy: Colv is FALSE, while dendrogram is `column'. Omitting column
## dendogram.
legend("topright", legend=levels(species),fill=levels(color0))

dataMatrix <- as.matrix(iris[,1:4])
species <- iris$Species
color0 <- species
levels(color0) <- palette()[1:length(levels(species))]
color <- as.character(color0)

heatmap.2(dataMatrix, trace = "none", RowSideColors = color,
           Colv=NA, Rowv = NA , scale = "row")
## Warning in heatmap.2(dataMatrix, trace = "none", RowSideColors = color, :
## Discrepancy: Rowv is FALSE, while dendrogram is `both'. Omitting row dendogram.
## Warning in heatmap.2(dataMatrix, trace = "none", RowSideColors = color, :
## Discrepancy: Colv is FALSE, while dendrogram is `column'. Omitting column
## dendogram.
legend("topright", legend=levels(species),fill=levels(color0))

dataMatrix <- as.matrix(iris[,1:4])
species <- iris$Species
color0 <- species
levels(color0) <- palette()[1:length(levels(species))]
color <- as.character(color0)

heatmap.2(dataMatrix, trace = "none", 
          RowSideColors = color, col = bluered, 
           Colv=NA, Rowv = NA , scale = "row")
## Warning in heatmap.2(dataMatrix, trace = "none", RowSideColors = color, :
## Discrepancy: Rowv is FALSE, while dendrogram is `both'. Omitting row dendogram.
## Warning in heatmap.2(dataMatrix, trace = "none", RowSideColors = color, :
## Discrepancy: Colv is FALSE, while dendrogram is `column'. Omitting column
## dendogram.
legend("topright", legend=levels(species),fill=levels(color0))

Adjust legend positions

dataMatrix <- as.matrix(iris[,1:4])
species <- iris$Species
color0 <- species
levels(color0) <- palette()[1:length(levels(species))]
color <- as.character(color0)

heatmap.2(dataMatrix, trace = "none", 
          RowSideColors = color, col = bluered, 
           Colv=NA, Rowv = NA , scale = "row", 
          margins = c(6,6))
## Warning in heatmap.2(dataMatrix, trace = "none", RowSideColors = color, :
## Discrepancy: Rowv is FALSE, while dendrogram is `both'. Omitting row dendogram.
## Warning in heatmap.2(dataMatrix, trace = "none", RowSideColors = color, :
## Discrepancy: Colv is FALSE, while dendrogram is `column'. Omitting column
## dendogram.
par(xpd=TRUE) ## default is FALSE, allow legend outside the plotting area
legend(x = 0.7, y = 1.1, legend=levels(species),fill=levels(color0))

Advanced heatmap