R基礎(chǔ)-05-熱圖-2019-07-08

R基礎(chǔ)-05-熱圖

主要通過(guò)學(xué)習(xí)example侠坎,掌握學(xué)習(xí)R包的學(xué)習(xí)方法;用pheatmap畫(huà)圖;

rm(list = ls())
library(pheatmap)

a1=rnorm(100);
dim(a1)=c(5,20);
pheatmap(a1)# Draw heatmaps;
a2 = rnorm(100) + 2 #or a2=a1+2;# Create test matrix;
b=cbind(a1,a2)
dim(a2)=c(5,20)
pheatmap(a2)

b = cbind(a1,a2)
pheatmap(cbind(a1,a2),cluster_cols = F)

b = as.data.frame(b)
paste('a1',1:20)#鏈接
paste('a1',1:20, sep = '20')
c(paste('a1',1:20, sep = '20'),paste('a2',1:20, sep = '20'))#a1和a2鏈接

names(b)= c(paste('a1',1:20, sep = '20'),paste('a2',1:20, sep = '20'))

pheatmap(b,cluster_cols = F)

?pheatmap



#b=as.data.frame(b),
#names(b)=c(paste(a1,1:20,sep=''), paste(a2, 1:20, sep=''))
#pheatmap(b, cluster_rows=F, cluster_cols=F);
#as.data.frame(group=c(rep("a1",20),rep("a2",20)))
#rownames(b)<-colnames(a);
#scale(log2(expr+1))
# Show text within cells;
# Fix cell sizes and save to file with correct size;
# Generate annotations for rows and columns;# Display row and color annotations;

?pheatmap
找到Examples,運(yùn)行代碼葬毫,參考數(shù)據(jù)框的畫(huà)圖方式央勒,繪制數(shù)據(jù)框的熱圖

繪制數(shù)據(jù)框的熱圖

tmp =data.frame(group=c(rep('a1',20),rep('a2',20)))#構(gòu)建數(shù)據(jù)框
rownames(tmp)=colnames(b)#把列名賦給行名
pheatmap(b,annotation_col = tmp)
Examples
# 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 = "")

# Draw heatmaps
pheatmap(test)
pheatmap(test, kmeans_k = 2)
pheatmap(test, scale = "row", clustering_distance_rows = "correlation")
pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50))
pheatmap(test, cluster_row = FALSE)
pheatmap(test, legend = FALSE)

# Show text within cells
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"))

# 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")

# Generate annotations for rows and columns
annotation_col = data.frame(
  CellType = factor(rep(c("CT1", "CT2"), 5)), 
  Time = 1:5
)
rownames(annotation_col) = paste("Test", 1:10, sep = "")

annotation_row = data.frame(
  GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6)))
)
rownames(annotation_row) = paste("Gene", 1:20, sep = "")

# Display row and color annotations
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)

# 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")

# Specify colors
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]) 

# Gaps in heatmaps
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)

# Show custom strings as row/col names
labels_row = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
               "", "", "Il10", "Il15", "Il1b")

pheatmap(test, annotation_col = annotation_col, labels_row = labels_row)

# 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)

# 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)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖门躯,帶你破解...
    沈念sama閱讀 211,290評(píng)論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異酷师,居然都是意外死亡讶凉,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,107評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門(mén)山孔,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)懂讯,“玉大人,你說(shuō)我怎么就攤上這事台颠『滞” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 156,872評(píng)論 0 347
  • 文/不壞的土叔 我叫張陵串前,是天一觀(guān)的道長(zhǎng)瘫里。 經(jīng)常有香客問(wèn)我,道長(zhǎng)荡碾,這世上最難降的妖魔是什么谨读? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,415評(píng)論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮坛吁,結(jié)果婚禮上劳殖,老公的妹妹穿的比我還像新娘贼邓。我一直安慰自己,他們只是感情好闷尿,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,453評(píng)論 6 385
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著女坑,像睡著了一般填具。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上匆骗,一...
    開(kāi)封第一講書(shū)人閱讀 49,784評(píng)論 1 290
  • 那天劳景,我揣著相機(jī)與錄音,去河邊找鬼碉就。 笑死盟广,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的瓮钥。 我是一名探鬼主播筋量,決...
    沈念sama閱讀 38,927評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼碉熄!你這毒婦竟也來(lái)了桨武?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 37,691評(píng)論 0 266
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤锈津,失蹤者是張志新(化名)和其女友劉穎呀酸,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體琼梆,經(jīng)...
    沈念sama閱讀 44,137評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡性誉,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,472評(píng)論 2 326
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了茎杂。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片错览。...
    茶點(diǎn)故事閱讀 38,622評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖蛉顽,靈堂內(nèi)的尸體忽然破棺而出蝗砾,到底是詐尸還是另有隱情,我是刑警寧澤携冤,帶...
    沈念sama閱讀 34,289評(píng)論 4 329
  • 正文 年R本政府宣布悼粮,位于F島的核電站,受9級(jí)特大地震影響曾棕,放射性物質(zhì)發(fā)生泄漏扣猫。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,887評(píng)論 3 312
  • 文/蒙蒙 一翘地、第九天 我趴在偏房一處隱蔽的房頂上張望申尤。 院中可真熱鬧癌幕,春花似錦、人聲如沸昧穿。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,741評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)时鸵。三九已至胶逢,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間饰潜,已是汗流浹背初坠。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,977評(píng)論 1 265
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留彭雾,地道東北人碟刺。 一個(gè)月前我還...
    沈念sama閱讀 46,316評(píng)論 2 360
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像薯酝,于是被迫代替她去往敵國(guó)和親半沽。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,490評(píng)論 2 348