R海拾遗---热图绘制-pheatmap

2020-09-15 12:35:56 浏览数 (1)

热图绘制-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函数都可生成一个图片,合适自己的才是最好的

0 人点赞