热图基础画法--pheatmap::pheatmap函数的Exa

2019-08-17  本文已影响0人  weixinsuoxian

热图:本质上它是表现一个数值矩阵,图上每一个小方格都是一个数值,按一条预设好的色彩变化尺(称为色键,Color Key),来给每个数值分配颜色,是对实验数据进行质制和差异数据的展现。

###Examples 软件包自带的实例
rm(list = ls())
options(stringsAsFactors = F)
# 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

library(pheatmap)
pheatmap(test)  #函数默认设置聚类 cluster_rows = TRUE,cluster_cols = TRUE
#把行归为2类进行聚类
pheatmap(test, kmeans_k = 2) 
#归一化(按照行),不同基因在不同样本中的表达量,可能会跨越好几个数量级,防止因极大值影响其他值的差异显现
pheatmap(test, scale = "row", clustering_distance_rows = "correlation")
#设定图表颜色,不设定则为默认颜色,50为50个色块
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)))
#只显示注释信息的-1到4,注释标签换成这几个数字
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
#设定宽,高,main为设定表的表头
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)),   #实验分组变量1,用颜色标出组别
  Time = 1:5                                    #实验分组变量2,用颜色标出组别
)
rownames(annotation_col) = paste("Test", 1:10, sep = "")  #"Test"的列名称赋给annotation_col

annotation_row = data.frame(
  GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6)))
)
rownames(annotation_row) = paste("Gene", 1:20, sep = "") #"Test"的行名称称赋给annotation_row

# Display row and color annotations
dev.off()    #关闭图形设备
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
#在第10行和14行,分开
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)
上一篇 下一篇

猜你喜欢

热点阅读