背景
畫熱圖的體系用的比較多的是pheatmap和ComplexHeatmap這兩個包,前者勝在代碼簡單,功能強大,而后者勝在細節(jié)無窮無盡,只有你想不到,沒有它做不到。
ggplot2在畫熱圖這件事上,是存在感不太強的。但有時候還必須得用它來畫,以期和其他ggplot2的圖嚴絲合縫的拼在一起。
因此我收集了一下ggplot2的成果,發(fā)現(xiàn)又解鎖了y叔的一個新包aplot,以及前段時間剛出的ggheatmap(居然是大三的學生寫的,后生可畏)。我寫了三種方法,ggheatmap最為簡單,可以直接去看方法3。
畫圖數(shù)據(jù)
熱圖的輸入數(shù)據(jù)嘛,是一個數(shù)值型矩陣:
test = matrix(rnorm(200), 20, 10)
test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3
test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2
test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4
colnames(test) = paste("Test", 1:10, sep = "")
rownames(test) = paste("Gene", 1:20, sep = "")
方法1 ggh4x
給ggplot2加聚類樹的R包ggh4x,里面的scale_y_dendrogram函數(shù)。
library(ggh4x)
library(ggplot2)
library(tidyverse)
yclust <- hclust(dist(test))
xclust <- hclust(dist(t(test)))
p = test %>%
as.data.frame() %>%
rownames_to_column() %>%
pivot_longer(cols = 2:ncol(.),
names_to = "sample",
values_to = "exp") %>%
ggplot(aes(x = sample,y = rowname))+
geom_tile(aes(fill = exp))+
scale_fill_gradient2(midpoint = 2.5,
low = '#2fa1dd',
mid="white",
high = '#f87669') +
scale_y_dendrogram(hclust = yclust) +
scale_x_dendrogram(hclust = xclust,position = 'top') +
theme(panel.grid = element_blank(), panel.background = element_rect(fill = NA),
legend.background = element_rect(fill = NA), plot.background = element_rect(fill = NA),
axis.line = element_blank(), axis.ticks = element_blank(),
axis.title = element_blank())
p

圖還是有模有樣的,只是行名列名貼著聚類樹,不如pheatmap畫的好看。所以我嘗試了一下把基因名放到右邊,失敗遼。但是又找到了另外一個做法:
方法2 ggtree
還是神奇Y叔的包,樹狀圖可視化用的ggtree,配合拼圖用的aplot,簡直不要太方便。
p1 = test %>%
as.data.frame() %>%
rownames_to_column() %>%
pivot_longer(cols = 2:ncol(.),
names_to = "sample",
values_to = "exp") %>%
ggplot(aes(x = sample,y = rowname))+
geom_tile(aes(fill = exp))+
scale_fill_gradient2(midpoint = 2.5,
low = '#2fa1dd',
mid="white",
high = '#f87669') +
scale_y_discrete(position = "right")+
theme(panel.grid = element_blank(), panel.background = element_rect(fill = NA),
legend.background = element_rect(fill = NA), plot.background = element_rect(fill = NA),
axis.line = element_blank(), axis.ticks = element_blank(),
axis.title = element_blank())
library(ggtree)
p2<-ggtree(yclust)
p2+
geom_tiplab()+
xlim(NA,10)

p3<-ggtree(xclust)+layout_dendrogram()
p3+
geom_tiplab()+
xlim(NA,12)
library(aplot)
p1%>%
insert_left(p2,width = 0.2) %>%
insert_top(p3,height = 0.2)

本來以為拼圖需要把主體熱圖的行列順序調(diào)整一下才能拼,結(jié)果神奇的aplot能實現(xiàn)無縫連接,順序調(diào)整猶如merge函數(shù)一樣,自動搞定了嘿~
看到ggheatmap也用到了aplot包,應該是同一種方法咯。寫成新的函數(shù)無比方便
方法3 ggheatmap
試了一下行列聚類和加注釋條的操作,還是很絲滑的。只是annotation_color沒有默認顏色,我jio的作者可以在下一版里設(shè)置上默認值。
library("ggheatmap")
ggheatmap(test,cluster_rows = T,cluster_cols = T,
color = colorRampPalette(c("#2fa1dd", "white", "#f87669"))(100))
annotation_col = data.frame(
CellType = factor(rep(c("CT1", "CT2"), 5))
)
rownames(annotation_col) = paste("Test", 1:10, sep = "")
col <- list(CellType=c(CT1 = "#2fa1dd",CT2 = "#f87669"))
ggheatmap(test,cluster_rows = T,cluster_cols = T,
color = colorRampPalette(c("#2fa1dd", "white", "#f87669"))(100),
annotation_cols = annotation_col,
annotation_color = col,
scale = "row")

這個配色我太喜歡了,恨不得走到哪都帶著。