R語言繪圖包02--熱圖pheatmap


R語言繪圖包系列:


熱圖輸入的數(shù)據(jù)是數(shù)值型矩陣/數(shù)據(jù)框,顏色的變化展示數(shù)值的大小萧求。

# usage: 
pheatmap(mat, color = colorRampPalette(rev(brewer.pal(n = 7, name =
  "RdYlBu")))(100), kmeans_k = NA, breaks = NA, border_color = "grey60",
  cellwidth = NA, cellheight = NA, scale = "none", cluster_rows = TRUE,
  cluster_cols = TRUE, clustering_distance_rows = "euclidean",
  clustering_distance_cols = "euclidean", clustering_method = "complete",
  clustering_callback = identity2, cutree_rows = NA, cutree_cols = NA,
  treeheight_row = ifelse((class(cluster_rows) == "hclust") || cluster_rows,
  50, 0), treeheight_col = ifelse((class(cluster_cols) == "hclust") ||
  cluster_cols, 50, 0), legend = TRUE, legend_breaks = NA,
  legend_labels = NA, annotation_row = NA, annotation_col = NA,
  annotation = NA, annotation_colors = NA, annotation_legend = TRUE,
  annotation_names_row = TRUE, annotation_names_col = TRUE,
  drop_levels = TRUE, show_rownames = T, show_colnames = T, main = NA,
  fontsize = 10, fontsize_row = fontsize, fontsize_col = fontsize,
  angle_col = c("270", "0", "45", "90", "315"), display_numbers = F,
  number_format = "%.2f", number_color = "grey30", fontsize_number = 0.8
  * fontsize, gaps_row = NULL, gaps_col = NULL, labels_row = NULL,
  labels_col = NULL, filename = NA, width = NA, height = NA,
  silent = FALSE, na_col = "#DDDDDD", ...)
1. 生成用于繪圖的矩陣
# Create test matrix
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 = "")
View(test)
2. 繪制熱圖
pheatmap(test) #畫一個最簡單的熱圖
#  kmeans_k參數(shù):如果想要在繪制熱圖前對行進(jìn)行聚類,這個參數(shù)可以設(shè)置想要得到的kmeans clusters的數(shù)目榴徐。如果不設(shè)置,默認(rèn)為NA穆端,也就是不對行進(jìn)行聚類仿便。
pheatmap(test, kmeans_k = 2)
pheatmap(test, scale = "row", clustering_distance_rows = "correlation")
# scale參數(shù)設(shè)置是否進(jìn)行歸一化探越,默認(rèn)scale = "none"钦幔。設(shè)置scale = "row"是對行進(jìn)行歸一化常柄,設(shè)置scale = "column"是對列進(jìn)行歸一化西潘。
# clustering_distance_rows設(shè)置在對行進(jìn)行clustering時計算距離所使用的方法,clustering_distance_rows = "correlation"是用皮爾森相關(guān)相种。也可以設(shè)置clustering_distance_rows =  "euclidean"等品姓。
pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50))
pheatmap(test, cluster_row = FALSE)
pheatmap(test, legend = FALSE)
3. 在格子里添加文字
pheatmap(test, display_numbers = TRUE)
pheatmap(test, display_numbers = TRUE, number_format = "%.1e")
pheatmap(test, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test)))
pheatmap(test, cluster_row = FALSE, legend_breaks = -1:4, legend_labels = c("0",
"1e-4", "1e-3", "1e-2", "1e-1", "1"))
4. 固定格子大小并且以正確的大小保存圖片
# Fix cell sizes and save to file with correct size
pheatmap(test, cellwidth = 15, cellheight = 12, main = "Example heatmap")
pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 8, filename = "test.pdf") 
#以pdf的格式保存在工作目錄下了
5. 為行和列添加注釋??????
  • 5.1 生成行名和列名的annotation
annotation_col = data.frame(
                    CellType = factor(rep(c("CT1", "CT2"), 5)), 
                    Time = 1:5
                )
rownames(annotation_col) = paste("Test", 1:10, sep = "")
annotation_col
#        CellType Time
# Test1       CT1    1
# Test2       CT2    2
# Test3       CT1    3
# Test4       CT2    4
# Test5       CT1    5
# Test6       CT2    1
# Test7       CT1    2
# Test8       CT2    3
# Test9       CT1    4
# Test10      CT2    5

annotation_row = data.frame(
                    GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6)))
                )
rownames(annotation_row) = paste("Gene", 1:20, sep = "")
annotation_row
#        GeneClass
# Gene1      Path1
# Gene2      Path1
# Gene3      Path1
# Gene4      Path1
# Gene5      Path1
# Gene6      Path1
# Gene7      Path1
# Gene8      Path1
# Gene9      Path1
# Gene10     Path1
# Gene11     Path2
# Gene12     Path2
# Gene13     Path2
# Gene14     Path2
# Gene15     Path3
# Gene16     Path3
# Gene17     Path3
# Gene18     Path3
# Gene19     Path3
# Gene20     Path3
  • 5.2 展示annotation
pheatmap(test, annotation_col = annotation_col)
pheatmap(test, annotation_col = annotation_col, annotation_legend = FALSE)
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row)
6. 改變列的字體角度
# Change angle of text in the columns
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, angle_col = "45")
pheatmap(test, annotation_col = annotation_col, angle_col = "0")
7. 改變顏色
ann_colors = list(
    Time = c("white", "firebrick"),
    CellType = c(CT1 = "#1B9E77", CT2 = "#D95F02"),
    GeneClass = c(Path1 = "#7570B3", Path2 = "#E7298A", Path3 = "#66A61E")
)

pheatmap(test, annotation_col = annotation_col, annotation_colors = ann_colors, main = "Title")
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, 
         annotation_colors = ann_colors)
pheatmap(test, annotation_col = annotation_col, annotation_colors = ann_colors[2]) 
8. 分割熱圖(設(shè)置gaps)
pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14))
pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14), 
         cutree_col = 2)
9. 設(shè)置特定行名和列名
labels_row = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "Il10", "Il15", "Il1b")

pheatmap(test, annotation_col = annotation_col, labels_row = labels_row)
10. 設(shè)置特定聚類方式(Specifying clustering from distance matrix)
drows = dist(test, method = "minkowski")
dcols = dist(t(test), method = "minkowski")
pheatmap(test, clustering_distance_rows = drows, clustering_distance_cols = dcols)
11. Modify ordering of the clusters using clustering callback option
callback = function(hc, mat){
    sv = svd(t(mat))$v[,1]
    dend = reorder(as.dendrogram(hc), wts = sv)
    as.hclust(dend)
}

pheatmap(test, clustering_callback = callback)
## Not run: 
# Same using dendsort package
library(dendsort)

callback = function(hc, ...){dendsort(hc)}
pheatmap(test, clustering_callback = callback)

## End(Not run)
12. pheatmap圖片保存

1.保存對象

library(pheatmap)
xx <- pheatmap(test)

2. 打開圖形設(shè)備重新畫
pheatmap包使用的是grid圖形系統(tǒng)而非ggplot2,所以解決方法也是不同的艾猜。通過自定義函數(shù)來生成捻悯,也可一次繪制多個對象的圖形今缚。

save_pheatmap_pdf <- function(x, filename, width=7, height=7) {
   stopifnot(!missing(x))
   stopifnot(!missing(filename))
   pdf(filename, width=width, height=height)
   grid::grid.newpage()
   grid::grid.draw(x$gtable)
   dev.off()
}
save_pheatmap_pdf(xx, "test.pdf")
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末姓言,一起剝皮案震驚了整個濱河市蔗蹋,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌餐塘,老刑警劉巖皂吮,帶你破解...
    沈念sama閱讀 218,682評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蜂筹,死亡現(xiàn)場離奇詭異,居然都是意外死亡不翩,警方通過查閱死者的電腦和手機(jī)麻裳,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,277評論 3 395
  • 文/潘曉璐 我一進(jìn)店門掂器,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人灭必,你說我怎么就攤上這事乃摹。” “怎么了播歼?”我有些...
    開封第一講書人閱讀 165,083評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長叭莫。 經(jīng)常有香客問我烁试,道長减响,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,763評論 1 295
  • 正文 為了忘掉前任刊橘,我火速辦了婚禮颂鸿,結(jié)果婚禮上据途,老公的妹妹穿的比我還像新娘叙甸。我一直安慰自己,他們只是感情好熔萧,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,785評論 6 392
  • 文/花漫 我一把揭開白布佛致。 她就那樣靜靜地躺著辙谜,像睡著了一般。 火紅的嫁衣襯著肌膚如雪罐脊。 梳的紋絲不亂的頭發(fā)上蜕琴,一...
    開封第一講書人閱讀 51,624評論 1 305
  • 那天凌简,我揣著相機(jī)與錄音,去河邊找鬼藕施。 笑死,一個胖子當(dāng)著我的面吹牛润绵,可吹牛的內(nèi)容都是我干的胞谈。 我是一名探鬼主播,決...
    沈念sama閱讀 40,358評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼卿捎,長吁一口氣:“原來是場噩夢啊……” “哼径密!你這毒婦竟也來了享扔?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,261評論 0 276
  • 序言:老撾萬榮一對情侶失蹤籽懦,失蹤者是張志新(化名)和其女友劉穎氛魁,沒想到半個月后秀存,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,722評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡惫恼,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年祈纯,在試婚紗的時候發(fā)現(xiàn)自己被綠了洞就。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,030評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出拦惋,到底是詐尸還是另有隱情安寺,我是刑警寧澤,帶...
    沈念sama閱讀 35,737評論 5 346
  • 正文 年R本政府宣布言秸,位于F島的核電站举畸,受9級特大地震影響凳枝,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜叛买,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,360評論 3 330
  • 文/蒙蒙 一率挣、第九天 我趴在偏房一處隱蔽的房頂上張望辅辩。 院中可真熱鬧娃圆,春花似錦、人聲如沸撩鹿。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,941評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至叫搁,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間渴逻,已是汗流浹背惨奕。 一陣腳步聲響...
    開封第一講書人閱讀 33,057評論 1 270
  • 我被黑心中介騙來泰國打工梨撞, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人卧波。 一個月前我還...
    沈念sama閱讀 48,237評論 3 371
  • 正文 我出身青樓幽勒,卻偏偏與公主長得像,于是被迫代替她去往敵國和親锈颗。 傳聞我的和親對象是個殘疾皇子咪惠,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,976評論 2 355

推薦閱讀更多精彩內(nèi)容