2020-04-19

相关性热图、差异基因热图等
画热图的包:pheatmap包
pheatmap()函数:
usage:基因和样本都可以单独聚类,排序,聚类再分组,行列注释,配色调整,调整聚类线以及单元格的宽度和高度均可实现
pheatmap()

#构建测试数据
set.seed(1234)
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 = "")
head(test[,1:6])
#加载包:
library(pheatmap)
pheatmap(mat = test)

参数:

  • 基本参数:
    mat:要输入的矩阵(只能是matrix)
    scale:"row"对行进行归一化、"column"对列进行归一化
    main:热图的标题
pheatmap(mat = test,scale = "column")
pheatmap(mat = test,scale = "row") # 默认
pheatmap(mat = test,scale = "none")
  • 控制cell的大小:cellwidth、cellheight(分别设置小格子的行列的宽度)
    fontsize(用于控制行列的名字的大小)
    fontsize_row、fontsize_col
pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 10)
pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 5)
  • 控制text:display_numbers = TRUE,参数设定在每个热图格子中显示相应的数字
    number_color参数设置数值字体的颜色
    number_format:设置数字的显示格式:例如"%.1e"
pheatmap(test, display_numbers = TRUE,number_color = "blue")
pheatmap(test, display_numbers = TRUE, number_format = "%.1e")
pheatmap(test, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test))) #指定显示数字的格子
  • 行或者列的label: 默认值为输入矩阵的行列名
    angle_col:控制行列标签的角度,0, 45, 90, 270 and 315
    show_rownames、show_colnames:逻辑值,是否显示行列名
labels_row = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "Il10", "Il15", "Il1b")
pheatmap(test, labels_row = labels_row)
pheatmap(test, labels_row = labels_row,show_colnames = F)
  • 设置legend:legend:表示是否设置图例
    legend_labels:设置图例的标签
    legend_breaks:设定图例显示范围
pheatmap(test, cluster_row = FALSE, legend_breaks = -2:6)
pheatmap(test, cluster_row = FALSE, legend_breaks = -1:4,legend_labels = c("0", "1e-4", "1e-3", "1e-2", "1e-1", "1"))
  • 关于聚类的参数:一般都弄成默认
    clustering_method:设定不同聚类方法,默认为"complete",可以设定为'ward', 'ward.D', 'ward.D2', 'single', 'complete', 'average', 'mcquitty', 'median' or 'centroid'
    cluster_row ,cluster_col:行、列是否聚类,\默认为TRUE
    treeheight_row和treeheight_col:设定行和列聚类树的高度,默认为50
    clustering_distance_rows = "euclidean",
    clustering_distance_cols = "euclidean",
    clustering_callback = identity2
    cutree_rows, cutree_cols:表示把如图按照行、列分为xx个部分
    treeheight_row = ifelse((class(cluster_rows) == "hclust") || cluster_rows,50, 0),
    treeheight_col = ifelse((class(cluster_cols) == "hclust") || cluster_cols, 50, 0)
  • 设定color:
  • 设定行、列 annotation:
    annotation_row:注释行的数据框,必须和输入的矩阵的行名相同
    annotation_col:一个数据框,数据的列名必须和输入
    annotation_legend :设置注释信息的图例,逻辑值,表示是否显示注释的图例
 # Generate annotations for rows and columns
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 = "") 
#用于行注释的数据框的行名必须等于用于作图的矩阵的行名
# Display row and color annotations
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_row、gaps_col:只有在没有进行聚类时才可以使用gap,其传入的值为一个向量
pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14))
pheatmap(test, annotation_col = annotation_col, cluster_cols = FALSE, gaps_col = c(2,5))

你可能感兴趣的:(2020-04-19)