R语言绘图包02--热图pheatmap


R语言绘图包系列:

  • R语言绘图包01--优秀的拼图包patchwork

热图输入的数据是数值型矩阵/数据框,颜色的变化展示数值的大小。

# usage: 
pheatmap(mat, color = colorRampPalette(rev(brewer.pal(n = 7, name =
  "RdYlBu")))(100), kmeans_k = NA, breaks = NA, border_color = "grey60",
  cellwidth = NA, cellheight = NA, scale = "none", cluster_rows = TRUE,
  cluster_cols = TRUE, clustering_distance_rows = "euclidean",
  clustering_distance_cols = "euclidean", clustering_method = "complete",
  clustering_callback = identity2, cutree_rows = NA, cutree_cols = NA,
  treeheight_row = ifelse((class(cluster_rows) == "hclust") || cluster_rows,
  50, 0), treeheight_col = ifelse((class(cluster_cols) == "hclust") ||
  cluster_cols, 50, 0), legend = TRUE, legend_breaks = NA,
  legend_labels = NA, annotation_row = NA, annotation_col = NA,
  annotation = NA, annotation_colors = NA, annotation_legend = TRUE,
  annotation_names_row = TRUE, annotation_names_col = TRUE,
  drop_levels = TRUE, show_rownames = T, show_colnames = T, main = NA,
  fontsize = 10, fontsize_row = fontsize, fontsize_col = fontsize,
  angle_col = c("270", "0", "45", "90", "315"), display_numbers = F,
  number_format = "%.2f", number_color = "grey30", fontsize_number = 0.8
  * fontsize, gaps_row = NULL, gaps_col = NULL, labels_row = NULL,
  labels_col = NULL, filename = NA, width = NA, height = NA,
  silent = FALSE, na_col = "#DDDDDD", ...)
1. 生成用于绘图的矩阵
# 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 = "")
View(test)
2. 绘制热图
pheatmap(test) #画一个最简单的热图
#  kmeans_k参数:如果想要在绘制热图前对行进行聚类,这个参数可以设置想要得到的kmeans clusters的数目。如果不设置,默认为NA,也就是不对行进行聚类。
pheatmap(test, kmeans_k = 2)
pheatmap(test, scale = "row", clustering_distance_rows = "correlation")
# scale参数设置是否进行归一化,默认scale = "none"。设置scale = "row"是对行进行归一化,设置scale = "column"是对列进行归一化。
# clustering_distance_rows设置在对行进行clustering时计算距离所使用的方法,clustering_distance_rows = "correlation"是用皮尔森相关。也可以设置clustering_distance_rows =  "euclidean"等。
pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50))
pheatmap(test, cluster_row = FALSE)
pheatmap(test, legend = FALSE)
3. 在格子里添加文字
pheatmap(test, display_numbers = TRUE)
pheatmap(test, display_numbers = TRUE, number_format = "%.1e")
pheatmap(test, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test)))
pheatmap(test, cluster_row = FALSE, legend_breaks = -1:4, legend_labels = c("0",
"1e-4", "1e-3", "1e-2", "1e-1", "1"))
4. 固定格子大小并且以正确的大小保存图片
# Fix cell sizes and save to file with correct size
pheatmap(test, cellwidth = 15, cellheight = 12, main = "Example heatmap")
pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 8, filename = "test.pdf") 
#以pdf的格式保存在工作目录下了
5. 为行和列添加注释⚠️⚠️⚠️
  • 5.1 生成行名和列名的annotation
annotation_col = data.frame(
                    CellType = factor(rep(c("CT1", "CT2"), 5)), 
                    Time = 1:5
                )
rownames(annotation_col) = paste("Test", 1:10, sep = "")
annotation_col
#        CellType Time
# Test1       CT1    1
# Test2       CT2    2
# Test3       CT1    3
# Test4       CT2    4
# Test5       CT1    5
# Test6       CT2    1
# Test7       CT1    2
# Test8       CT2    3
# Test9       CT1    4
# Test10      CT2    5

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
#        GeneClass
# Gene1      Path1
# Gene2      Path1
# Gene3      Path1
# Gene4      Path1
# Gene5      Path1
# Gene6      Path1
# Gene7      Path1
# Gene8      Path1
# Gene9      Path1
# Gene10     Path1
# Gene11     Path2
# Gene12     Path2
# Gene13     Path2
# Gene14     Path2
# Gene15     Path3
# Gene16     Path3
# Gene17     Path3
# Gene18     Path3
# Gene19     Path3
# Gene20     Path3
  • 5.2 展示annotation
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)
6. 改变列的字体角度
# 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")
7. 改变颜色
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]) 
8. 分割热图(设置gaps)
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)
9. 设置特定行名和列名
labels_row = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "Il10", "Il15", "Il1b")

pheatmap(test, annotation_col = annotation_col, labels_row = labels_row)
10. 设置特定聚类方式(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)
11. Modify ordering of the clusters using clustering callback option
callback = function(hc, mat){
    sv = svd(t(mat))$v[,1]
    dend = reorder(as.dendrogram(hc), wts = sv)
    as.hclust(dend)
}

pheatmap(test, clustering_callback = callback)
## Not run: 
# Same using dendsort package
library(dendsort)

callback = function(hc, ...){dendsort(hc)}
pheatmap(test, clustering_callback = callback)

## End(Not run)
12. pheatmap图片保存

1.保存对象

library(pheatmap)
xx <- pheatmap(test)

2. 打开图形设备重新画
pheatmap包使用的是grid图形系统而非ggplot2,所以解决方法也是不同的。通过自定义函数来生成,也可一次绘制多个对象的图形。

save_pheatmap_pdf <- function(x, filename, width=7, height=7) {
   stopifnot(!missing(x))
   stopifnot(!missing(filename))
   pdf(filename, width=width, height=height)
   grid::grid.newpage()
   grid::grid.draw(x$gtable)
   dev.off()
}
save_pheatmap_pdf(xx, "test.pdf")

你可能感兴趣的:(R语言绘图包02--热图pheatmap)