目前R中繪制熱圖的方式有很多吁恍,常用的如pheatmap伴奥、ComplexHeatmap包等感局,這里再給大家介紹一個(gè)輕量級(jí)的R包-HeatmapR包崖瞭,即無需過多的前期數(shù)據(jù)處理唧领,可同時(shí)可視化含有離散型和連續(xù)性的矩陣西雀。
Github地址:https://github.com/DillonHammill/HeatmapR
準(zhǔn)備數(shù)據(jù)
## 按照R包
devtools::install_github("DillonHammill/HeatmapR")
library(HeatmapR)
## 準(zhǔn)備數(shù)據(jù)
data <- mtcars
data <- data[sample(nrow(data),20),]
head(data)
> str(data)
'data.frame': 20 obs. of 11 variables:
$ mpg : num 21.4 17.8 32.4 30.4 18.1 27.3 22.8 21 19.2 16.4 ...
$ cyl : num 4 6 4 4 6 4 4 6 8 8 ...
$ disp: num 121 167.6 78.7 75.7 225 ...
$ hp : num 109 123 66 52 105 66 93 110 175 180 ...
$ drat: num 4.11 3.92 4.08 4.93 2.76 4.08 3.85 3.9 3.08 3.07 ...
$ wt : num 2.78 3.44 2.2 1.61 3.46 ...
$ qsec: num 18.6 18.9 19.5 18.5 20.2 ...
$ vs : num 1 1 1 1 1 1 1 0 0 0 ...
$ am : num 1 0 1 1 0 1 1 1 0 0 ...
$ gear: num 4 4 4 4 3 4 4 4 3 3 ...
$ carb: num 2 4 1 2 1 1 1 4 2 3 ...
目前再悼,數(shù)據(jù)集里各列都是數(shù)值型的數(shù)據(jù)跟束,先簡單出圖看下效果灭贷。
基礎(chǔ)繪圖
library(HeatmapR)
heat_map(
data,
scale = "column",
scale_method = "range", # "range", "mean" or "zscore" 三種標(biāo)準(zhǔn)化方式
tree_x = TRUE, ## 顯示X軸聚類
tree_y = TRUE, ##顯示Y軸聚類
tree_cut_x = 4, ## 行分割數(shù)
tree_cut_y = 12, ##列分割數(shù)
cell_text = TRUE, ## 顯示數(shù)值
cell_text_col = 'black',
cell_size = TRUE, ## 控制大小
cell_shape = "diamond", #設(shè)置性狀
title = "mtcars"
)
添加缺失值并改變數(shù)據(jù)類型
此包的便捷性在于可將同時(shí)含有連續(xù)性和離散型矩陣可視化熱圖揽祥,這里隨機(jī)添加幾個(gè)缺失值,并改變cyl列的數(shù)據(jù)為因子類型盔然,然后再繪制熱圖看下效果桅打。
## 添加NA值并改變某列為因子型
data[c(2, 19, 14, 7), 3] <- NA
data[c(4, 9, 17, 20), 4] <- NA
data$cyl <- factor(data$cyl)
str(data)
> str(data)
'data.frame': 20 obs. of 11 variables:
$ mpg : num 21.4 17.8 32.4 30.4 18.1 27.3 22.8 21 19.2 16.4 ...
$ cyl : Factor w/ 3 levels "4","6","8": 1 NA 1 1 2 1 NA 2 3 3 ...
$ disp: num 121 167.6 78.7 NA 225 ...
$ hp : num 109 123 66 52 105 66 93 110 175 180 ...
$ drat: num 4.11 3.92 4.08 4.93 2.76 4.08 3.85 3.9 3.08 3.07 ...
$ wt : num 2.78 3.44 2.2 1.61 3.46 ...
$ qsec: num 18.6 18.9 19.5 18.5 20.2 ...
$ vs : num 1 1 1 1 1 1 1 0 0 0 ...
$ am : num 1 0 1 1 0 1 1 1 0 0 ...
$ gear: num 4 4 4 4 3 4 4 4 3 3 ...
$ carb: num 2 4 1 2 1 1 1 4 2 3 ...
## 繪制熱圖
heat_map(
data,
scale = "column",
scale_method = "range", # "range", "mean" or "zscore" 三種方式
tree_x = TRUE, ## 顯示X軸聚類
tree_y = TRUE, ##顯示Y軸聚類
tree_cut_x = 4,
tree_cut_y = 12,
cell_text = TRUE,
cell_text_col = 'black',
#cell_size = TRUE, ## 控制大小
cell_shape = "diamond", #設(shè)置性狀
title = "mtcars",
cell_col_empty = "red",
cell_col_scale = c( ##連續(xù)性數(shù)據(jù)配色
'#2ab49b',
'white',
'#ea7f58'
),
cell_col_palette = c( ##離散型數(shù)據(jù)配色
"#197fcd",
"#00c347",
"#fb3b65"
),
cell_col_alpha = 1
)
其它功能
添加柱狀圖
heat_map(
data,
cell_col_scale = c( ##連續(xù)性數(shù)據(jù)配色
'#2ab49b',
'white',
'#ea7f58'
),
scale = "column",
scale_method = "range",
bar_values_x = 1:ncol(mtcars),
bar_size_x = 0.5,
bar_fill_x = rainbow(ncol(mtcars)),
bar_line_col_x = "black",
bar_values_y = 1:nrow(mtcars),
bar_size_y = 0.8,
bar_fill_y = rainbow(ncol(mtcars)),
bar_line_col_y = "black",
title = "heatmap with bar plots"
)
該包參數(shù)非常多是嗜,也基本涵蓋了熱圖常用的調(diào)參細(xì)節(jié),如添加行與列分組注釋挺尾,聚類方法設(shè)置鹅搪,圖例與導(dǎo)出等等,感興趣的可以參考HeatmapR ? HeatmapR (dillonhammill.github.io)指南進(jìn)行學(xué)習(xí)遭铺,這里不再一一介紹了丽柿。