前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >热图绘制-pheatmap

热图绘制-pheatmap

原创
作者头像
Dr_Zhong
修改2021-05-25 10:27:42
1.6K0
修改2021-05-25 10:27:42
举报
文章被收录于专栏:生信学习笔记生信学习笔记

热图绘制-pheatmap

概述

新买的蓝牙耳机到了,试了试感觉还不错,低音也非常出色,窗外的颜色变得丰富了起来,看着街角那家咖啡店,仿佛回到了昨天,血色染红的天空在斑斓的世界之上,我匆匆茫茫的写下“这把火在我心底永远不会熄灭”。

代码

安装和调用

代码语言:javascript
复制
install.packages("pheatmap")
library(pheatmap)
# 建立测试数据集
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 = "")

# 结果为20行10列的数据集
# 绘图
pheatmap(test)
代码语言:javascript
复制
# 进行聚合,聚为2
pheatmap(test, kmeans_k = 2)
代码语言:javascript
复制
# 是否进行标准化,距离的选择
pheatmap(test, scale = "row", clustering_distance_rows = "correlation")
代码语言:javascript
复制
# 颜色调试
pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50))
代码语言:javascript
复制
# 是否对行进行聚类
pheatmap(test, cluster_row = FALSE)
代码语言:javascript
复制
# 是否显示图例
pheatmap(test, legend = FALSE)
代码语言:javascript
复制
# cells中显示数值
pheatmap(test, display_numbers = TRUE)
代码语言:javascript
复制
# 数字的格式
pheatmap(test, display_numbers = TRUE, number_format = "\ %.1e")
# 对于大于5的cells加星号
pheatmap(test, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test)))

后面涉及一些微小的改变,就不粘贴图片了,有兴趣可以粘贴代码去试试

代码语言:javascript
复制
# 对于图例进行调整
pheatmap(test, cluster_row = FALSE, legend_breaks = -1:4, legend_labels = c("0","1e-4", "1e-3", "1e-2", "1e-1", "1"))

# 建立注释集
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 = "")


annotation_row
# 显示行和颜色注释
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)

# 更改列中字符的角度
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, angle_col = "45")
# 更改列角度为0
pheatmap(test, annotation_col = annotation_col, angle_col = "0")

# 建立颜色数据集
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])

# 增加一个gap
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)

# 自定义行名
labels_row = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "","", "", "Il10", "Il15", "Il1b")
pheatmap(test, annotation_col = annotation_col, labels_row = labels_row)

# 指定聚类的方法
drows = dist(test, method = "minkowski")
dcols = dist(t(test), method = "minkowski")
pheatmap(test, clustering_distance_rows = drows, clustering_distance_cols = dcols)

结束语

每一个pheatmap函数都可生成一个图片,合适自己的才是最好的

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
作者已关闭评论
0 条评论
热度
最新
推荐阅读
目录
  • 热图绘制-pheatmap
    • 概述
      • 代码
        • 结束语
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
        http://www.vxiaotou.com