Load Packages
library(devtools)
## Loading required package: usethis
library(ComplexHeatmap)
## Loading required package: grid
## ========================================
## ComplexHeatmap version 2.12.1
## Bioconductor page: http://bioconductor.org/packages/ComplexHeatmap/
## Github page: https://github.com/jokergoo/ComplexHeatmap
## Documentation: http://jokergoo.github.io/ComplexHeatmap-reference
## 
## If you use it in published research, please cite either one:
## - Gu, Z. Complex heatmaps reveal patterns and correlations in multidimensional 
##     genomic data. Bioinformatics 2016.
## - Gu, Z. Complex Heatmap Visualization. iMeta 2022.
## 
## 
## The new InteractiveComplexHeatmap package can directly export static 
## complex heatmaps into an interactive Shiny app with zero effort. Have a try!
## 
## This message can be suppressed by:
##   suppressPackageStartupMessages(library(ComplexHeatmap))
## ========================================
library(circlize)
## ========================================
## circlize version 0.4.15
## CRAN page: https://cran.r-project.org/package=circlize
## Github page: https://github.com/jokergoo/circlize
## Documentation: https://jokergoo.github.io/circlize_book/book/
## 
## If you use it in published research, please cite:
## Gu, Z. circlize implements and enhances circular visualization
##   in R. Bioinformatics 2014.
## 
## This message can be suppressed by:
##   suppressPackageStartupMessages(library(circlize))
## ========================================
library(hopach)
## Loading required package: cluster
## Loading required package: Biobase
## Loading required package: BiocGenerics
## 
## Attaching package: 'BiocGenerics'
## The following objects are masked from 'package:stats':
## 
##     IQR, mad, sd, var, xtabs
## The following objects are masked from 'package:base':
## 
##     anyDuplicated, append, as.data.frame, basename, cbind, colnames,
##     dirname, do.call, duplicated, eval, evalq, Filter, Find, get, grep,
##     grepl, intersect, is.unsorted, lapply, Map, mapply, match, mget,
##     order, paste, pmax, pmax.int, pmin, pmin.int, Position, rank,
##     rbind, Reduce, rownames, sapply, setdiff, sort, table, tapply,
##     union, unique, unsplit, which.max, which.min
## Welcome to Bioconductor
## 
##     Vignettes contain introductory material; view with
##     'browseVignettes()'. To cite Bioconductor, see
##     'citation("Biobase")', and for packages 'citation("pkgname")'.

Visualize the mtcars dataset using hierarchical cluster analysis heatmaps

head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
data<-mtcars[,c(1:7)]
data.scale<-scale(data)
head(data.scale)
##                          mpg        cyl        disp         hp       drat
## Mazda RX4          0.1508848 -0.1049878 -0.57061982 -0.5350928  0.5675137
## Mazda RX4 Wag      0.1508848 -0.1049878 -0.57061982 -0.5350928  0.5675137
## Datsun 710         0.4495434 -1.2248578 -0.99018209 -0.7830405  0.4739996
## Hornet 4 Drive     0.2172534 -0.1049878  0.22009369 -0.5350928 -0.9661175
## Hornet Sportabout -0.2307345  1.0148821  1.04308123  0.4129422 -0.8351978
## Valiant           -0.3302874 -0.1049878 -0.04616698 -0.6080186 -1.5646078
##                             wt       qsec
## Mazda RX4         -0.610399567 -0.7771651
## Mazda RX4 Wag     -0.349785269 -0.4637808
## Datsun 710        -0.917004624  0.4260068
## Hornet 4 Drive    -0.002299538  0.8904872
## Hornet Sportabout  0.227654255 -0.4637808
## Valiant            0.248094592  1.3269868
Heatmap(data.scale, name="mtcar heatmap",
        cluster_rows=FALSE, cluster_columns=FALSE)

Heatmap(data.scale, name = "mtcars heatmap", 
        cluster_columns = FALSE)

Heatmap(data.scale,name="mtcars heatmap")

uncenter.dist<-function(m){
  as.dist(as.matrix(distancematrix(m,d="cosangle")))
}
row.clus<-hclust(uncenter.dist(data.scale),method="ave")
                 col.clus<-hclust(uncenter.dist(t(data.scale)),method="ave")

Heatmap(data.scale, name = "mtcars heatmap", 
        cluster_rows=row.clus, cluster_columns=col.clus)

ht_inline<-Heatmap(mtcars[, c(8)],
        name = "Cylinder Config",
        col=colorRamp2(c(0,1),c("white", "black")),
        heatmap_legend_param = list(at = c(0,1),
        labels = c("V-shaped","Inline")),
                   width = unit(0.5,"cm"))

ht_manual<-Heatmap(mtcars[,c(9)], name = "Transmission",
        col = colorRamp2(c(0,1),c("white", "black")),
      heatmap_legend_param = list(at = c(0,1),
      labels = c("Automatic", "Manual")),
      width = unit(0.5,"cm"))
ht_main = Heatmap(data.scale, name = "cluster rows",
  cluster_rows=row.clus, cluster_columns = col.clus)

draw(ht_main+ht_manual+ht_inline, auto_adjust = FALSE) 

draw(ht_main+ht_manual+ht_inline) 

Use Complex Heatmap to visualize the mtcars dataset.

head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
data<-mtcars[,c(1:7)]
data.scale<-scale(data)
head(data.scale)
##                          mpg        cyl        disp         hp       drat
## Mazda RX4          0.1508848 -0.1049878 -0.57061982 -0.5350928  0.5675137
## Mazda RX4 Wag      0.1508848 -0.1049878 -0.57061982 -0.5350928  0.5675137
## Datsun 710         0.4495434 -1.2248578 -0.99018209 -0.7830405  0.4739996
## Hornet 4 Drive     0.2172534 -0.1049878  0.22009369 -0.5350928 -0.9661175
## Hornet Sportabout -0.2307345  1.0148821  1.04308123  0.4129422 -0.8351978
## Valiant           -0.3302874 -0.1049878 -0.04616698 -0.6080186 -1.5646078
##                             wt       qsec
## Mazda RX4         -0.610399567 -0.7771651
## Mazda RX4 Wag     -0.349785269 -0.4637808
## Datsun 710        -0.917004624  0.4260068
## Hornet 4 Drive    -0.002299538  0.8904872
## Hornet Sportabout  0.227654255 -0.4637808
## Valiant            0.248094592  1.3269868
col_fun = colorRamp2(c(-4, 4), c("yellow", "blue"))
col_fun(seq(-3, 3))
## [1] "#F5E153FF" "#EAC476FF" "#DBA792FF" "#CA8AAAFF" "#B46DC0FF" "#9850D5FF"
## [7] "#7030EAFF"
Heatmap(data.scale, name = "Car Heatmap",col = col_fun)

Heatmap(data.scale, name = "Car Heatmap", rect_gp = gpar(col = "white", lwd = 2))

col_fun = colorRamp2(c(-4, 4), c("yellow", "blue"))
col_fun(seq(-3, 3))
## [1] "#F5E153FF" "#EAC476FF" "#DBA792FF" "#CA8AAAFF" "#B46DC0FF" "#9850D5FF"
## [7] "#7030EAFF"
Heatmap(data.scale, name = "Car Heatmap",
    row_km = 3, row_gap = unit(c(2, 4), "mm"),
    column_km = 3, column_gap = unit(c(2, 4), "mm"))

col_fun = colorRamp2(c(-4, 4), c("yellow", "blue"))
col_fun(seq(-3, 3))
## [1] "#F5E153FF" "#EAC476FF" "#DBA792FF" "#CA8AAAFF" "#B46DC0FF" "#9850D5FF"
## [7] "#7030EAFF"
Heatmap(data.scale, name = "Car Heatmap",
    row_km = 2, column_km = 3, border = TRUE)