pheatmap包來顯示行或列的分組信息
行或列的分組注釋信息
在EXCEL中整理好樣本和物種的注釋信息,當然這個需要依據(jù)你的具體需求來確定盹愚。
對樣本的注釋信息栅迄,比如這里用到了采樣環(huán)境,和樣本分類
對屬水平物種的屬于哪個門水平的注釋皆怕,你可以用其他方式來區(qū)分或不做注釋
注意:將上述的注釋信息保存為文本文件(制表符格式)毅舆,當然你可以保存為其他格式,但在R語言中需要使用不同的函數(shù)來將數(shù)據(jù)導入端逼。
導入行或列的分組注釋信息
在RStudio 中使用 read.table 函數(shù)來導入行或列的分組注釋信息
annotation_row<-read.table("phylum.txt",header=T,sep="\t",row.names=1)
# 這些需要將導入的注釋信息轉(zhuǎn)化為data.frame 格式
annotation_row <- as.data.frame(annotation_row)
annotation_col<-read.table("樣品信息.txt",header=T,sep="\t",row.names=1)
annotation_col <- as.data.frame(annotation_col)
# 查看行或列注釋信息的導入情況朗兵,我比較喜歡view,也可以使用head函數(shù)查看前6行顶滩。
View(annotation_col)
View(annotation_row)
出圖查看注釋情況
注意:data.1是pheatmap包基礎(chǔ)篇中介紹過使用z-score中心化和標準化的數(shù)據(jù)余掖;
具體可以參考上一期內(nèi)容
# data.1是pheatmap包基礎(chǔ)篇中介紹過使用z-score中心化和標準化的數(shù)據(jù)
# annotation_col = annotation_col #添加列注釋信息
# annotation_row = annotation_row 添加行注釋信息
# 這里注意橫縱的注釋信息要對好,別錯了
pheatmap(data.1, annotation_col = annotation_col, annotation_row = annotation_row,
cellwidth = 20, cellheight = 15)
常見報錯
Error in names(annotation_colors[[names(annotation)[i]]]) <- l :
'names' attribute [2] must be the same length as the vector [1]
如果出圖出現(xiàn)這個報錯,一般是橫縱注釋信息數(shù)量不一致盐欺,應該是你橫縱注釋信息放錯位置了赁豆,可以調(diào)換一下順序。
修改注釋信息及顏色參數(shù)
使用annotation_legend = FALSE 命令去掉注釋圖例
pheatmap(data.1, annotation_col = annotation_col, annotation_row = annotation_row,
cellwidth = 20, cellheight = 15,
annotation_legend = FALSE)
自定注釋信息的顏色列表
ann_colors = list(
species= c("white", "tomato4"),
environment = c(X1 = "slateblue3", X2 = "red2"),
phylum = c(phylum1 = "#7D26CD", phylum2 = "#E7298A", phylum3 = "#66A61E")
)
head(ann_colors)
使用 annotation_colors 設定注釋信息的顏色
# annotation_colors設定注釋信息的顏色
pheatmap(data.1, annotation_col = annotation_col,
annotation_row = annotation_row,
annotation_colors = ann_colors,
cellwidth = 20, cellheight = 15)
這是注釋顏色冗美,好像不是特別好看魔种,根據(jù)自己美感慢慢調(diào)節(jié)
annotation_colors = ann_colors[1] 只修改species的注釋顏色,其他保持不變
pheatmap(data.1, annotation_col = annotation_col,
annotation_row = annotation_row,
annotation_colors = ann_colors[1],
cellwidth = 20, cellheight = 15)
將熱圖分隔開
使用 cutree_rows, cutree_cols 函數(shù)可以根據(jù)行列的聚類數(shù)將熱圖分隔開
pheatmap(data.1,cutree_rows=3,cutree_cols=3,
cellwidth = 20, cellheight = 15)
使用gaps_row 和gaps_col 函數(shù)可以在指定位置處添加gap
# gaps_row = c(7, 17)參數(shù)在第7和17行處添加gap, 同時對行不進行聚類
pheatmap(data.1, annotation_col = annotation_col,
cluster_rows = FALSE, gaps_row = c(7, 17),
cellwidth = 20, cellheight = 15)
對行和列都不進行聚類粉洼,然后自定義劃分行和列的gap
# 對行和列都不聚類节预,自定義劃分行和列的gap
pheatmap(data.1, annotation_col = annotation_col,
annotation_row = annotation_row,
cluster_rows = FALSE, cluster_cols = FALSE,
gaps_row = c(12, 21), gaps_col = c(5),
cellwidth = 20, cellheight = 15)
注意:紅色框不是r生成的,而是截圖后手動添加属韧,為了突出自定義的行和列
自定義行列標簽名
自定義行的標簽名
labels_row = c("genus1", "genus2", "genus3", "genus4", " ", " ", " ", " ", " ", " ",
" ", " ", " ", " ", " ", " ", " ", " ", " ", " ",
" ", " ", " ", " "," "," ", " ", "genus26", "genus27", "genus28")
使用labels_row參數(shù)來添加行標簽
# 使用labels_row參數(shù)來添加行標簽安拟,但是最好把行的聚類給去除
pheatmap(data.1, annotation_col = annotation_col, labels_row = labels_row,
cluster_rows = FALSE,
cellwidth = 20, cellheight = 15)
使用filename 參數(shù)輸出圖片進行保存為pdf
pheatmap(data.1, annotation_col = annotation_col,
annotation_row = annotation_row,
annotation_colors = ann_colors[1],
cellwidth = 20, cellheight = 15,
filename = "data.pdf")
將熱圖的結(jié)果,按照聚類后的順序進行輸出
result =pheatmap(data.1, annotation_col = annotation_col,
annotation_row = annotation_row,
annotation_colors = ann_colors[1],
cellwidth = 20, cellheight = 15)
# 簡要查看熱圖對象的信息
> summary(result)
將熱圖的結(jié)果宵喂,按照聚類后的順序進行輸出
order_row = result$tree_row$order #記錄熱圖聚類后的行排序
order_col = result$tree_col$order #記錄熱圖聚類后的列排序
data.2 = data.frame(data[order_row,
order_col]) # 按照熱圖聚類后的順序糠赦,重新排列原始數(shù)據(jù)
data.2 = data.frame(rownames(data.2),
data.2,check.names =F) # 將物種屬名的行名加到表格數(shù)據(jù)中
colnames(data.2)[1] = "genus"
write.table(data.2,file="genus聚類后數(shù)據(jù).txt",row.names=FALSE,
quote = FALSE,sep='\t') #輸出結(jié)果,保存到當前工作目錄
運用R語言 pheatmap 包繪制熱圖進階部分的內(nèi)容就到這結(jié)束了锅棕。
如有不足或錯誤之處拙泽,請批評指正。
有什么不明白的也歡迎留言討論裸燎。
感謝你的閱讀9苏啊!德绿!
歡迎關(guān)注微信公眾號:fafu 生信 小蘑菇