R语言绘制热图——pheatmap

pheatmap简介: Pretty Heatmaps——Implementation of heatmaps that offers more control over dimensions and appearance.

[plain]  view plain  copy
  1. library(pheatmap)  
  2. #创建数据集test测试矩阵  
  3. test = matrix(rnorm(200), 20, 10)  
  4. test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3  
  5. test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2  
  6. test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4  
  7. colnames(test) = paste("Test", 1:10, sep = "")  
  8. rownames(test) = paste("Gene", 1:20, sep = "")  
  9. # 用pheatmap函数画热图  
  10. pheatmap(test)  
#默认参数下是对行列均进行聚类(可设置cluster_row = FALSE, cluster_col = FALSE不进行行列的聚类;如果进行聚类了,还可以通过设置treeheight_row=0, treeheight_col=0不显示dendrogram),矩阵没有进行标准化(标准化参数为scale,可选"none", "row", "column"),热图的每个小块之间以灰色隔开(参数border_color,如果不想要border可以设置为NA,当然也可以设置成其它颜色),legend显示在右上方(可设置legend = FALSE不显示legend);热图的颜色可利用参数color调整;

[plain]  view plain  copy
  1. <table class="NormalTable  "><tbody><tr><td width="534"> </td></tr><tr><td width="534"><span class="fontstyle0">  
  2. </span><span style="font-family: Simsun;font-size:14px;">pheatmap(test, cluster_row = FALSE, legend_breaks = -1:4, legend_labels = c("0",</span></td></tr></tbody></table>  
[plain]  view plain  copy
  1. "1e-4", "1e-3", "1e-2", "1e-1", "1"))#可自己设置图例  
#可设置参数display_numbers将数值显示在热图的格子中,可通过number_format设置数值的格式,较常用的有"%.2f"(保留小数点后两位),"%.1e"(科学计数法显示,保留小数点后一位),number_color设置显示内容的颜色:
pheatmap(test, display_numbers = TRUE, number_format = "%.2f", number_color="purple") #"%.2f"表示保留小数点后两位
[plain]  view plain  copy
  1. # 在热图格子里展示文本  
  2. pheatmap(test, display_numbers = TRUE)  
  3. pheatmap(test, display_numbers = TRUE, number_format = "\%.1e")  
  4. pheatmap(test, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test)))#还可以自己设定要显示的内容;  



#pheatmap参数设置改变每个格子的大小
pheatmap(test, cellwidth = 15, cellheight = 12, main = "Example heatmap", fontsize = 8, filename = "test.pdf") #main可设置热图的标题,fontsize设置字体大小,filename可直接将热图存出,支持格式png, pdf, tiff, bmp, jpeg,并且可以通过width, height设置图片的大小;
[plain]  view plain  copy
  1. #pheatmap还可以显示行或列的分组信息,支持多种分组;  
  2. annotation_col = data.frame(CellType = factor(rep(c("CT1", "CT2"), 5)), Time = 1:5)  
  3. rownames(annotation_col) = paste("Test", 1:10, sep = "")  
  4.    
  5. annotation_row = data.frame(GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6))))  
  6. rownames(annotation_row) = paste("Gene", 1:20, sep = "")  
  7.    
  8. pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row)  


[plain]  view plain  copy
  1. #设定各个分组的颜色  
  2. ann_colors = list(Time = c("white", "firebrick"), #连续数值型分组可设置成渐变  
  3.     CellType = c(CT1 = "#1B9E77", CT2 = "#D95F02"),  
  4.     GeneClass = c(Path1 = "#7570B3", Path2 = "#E7298A", Path3 = "#66A61E"))  
  5. pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row,  
  6.          annotation_colors = ann_colors)  
  7.    


#pheatmap还能够根据特定的条件将热图分隔开;
# cutree_rows, cutree_cols:根据行列的聚类数将热图分隔开;
pheatmap(test,cutree_rows=3,cutree_cols=2)

[plain]  view plain  copy
  1. #还可以自己设定各个分组的颜色  
  2. ann_colors = list(Time = c("white", "firebrick"), #连续数值型分组可设置成渐变  
  3.     CellType = c(CT1 = "#1B9E77", CT2 = "#D95F02"),  
  4.     GeneClass = c(Path1 = "#7570B3", Path2 = "#E7298A", Path3 = "#66A61E"))  
  5. pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row,  
  6.          annotation_colors = ann_colors)  
  7.  #还可以利用gaps_row, gaps_col自己设定要分隔开的位置  
  8. pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14),  
  9.          cutree_col = 2)  





转载自:http://blog.csdn.net/sinat_38163598/article/details/72770404

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值