初学者R语言:热图基础画法及个性化调整详解

热图(Heatmap):用颜色变化直观的表达数据之间差异的图,是对实验数据进行质制和差异数据的展现,是数据挖掘类文章的标配。

Rplot1.jpeg

例如上图,每个小方格表示每个基因,其颜色表示该基因表达量大小,表达量越大颜色越深(红色为上调,蓝色为下调)。每行表示每个基因在不同样本中的表达量情况,每列表示每个样品中所有基因的表达量情况。上方树形图表示对来自不同实验分组的不同样品的聚类分析结果

Draw heatmaps#画热图

pheatmap(test)
pheatmap(test, kmeans_k = 2)#把所有行归为两类画热图
pheatmap(test, scale = "row")#归一化(按照行),防止因极大值使其他值的差异显现不出来
pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50))#设定图表颜色,不设定则为默认颜色,50为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)))#大于5的*号
pheatmap(test, cluster_row = FALSE,  legend_breaks= -1:4,)
)
legend_breaks= -1:4#只显示注释信息的-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#修复单元格大小并以正确的大小保存到文件

pheatmap(test, cellwidth = 15, cellheight = 12,main = "Example heatmap")#设定宽,高,main为设定表的表头,也就是名字
pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 8, filename = "test.pdf")#fontsize设定字体大小,filename设定文件名

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")#下标文本倾斜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")
)

分别设定3种注释不同的颜色

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
pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14))#在第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)
pheatmap(test)
pheatmap(test, kmeans_k = 2)#把所有行归为两类画热图
pheatmap(test, scale = "row")#归一化(按照行),防止因极大值使其他值的差异显现不出来
pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50))#设定图表颜色,不设定则为默认颜色,50为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)))#大于5的*号
pheatmap(test, cluster_row = FALSE,  legend_breaks= -1:4,)
)
,#legend_breaks= -1:4#只显示注释信息的-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#修复单元格大小并以正确的大小保存到文件

pheatmap(test, cellwidth = 15, cellheight = 12,main = "Example heatmap")#设定宽,高,main为设定表的表头,也就是名字
pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 8, filename = "test.pdf")#fontsize设定字体大小,filename设定文件名

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")#下标文本倾斜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")
)#分别设定3种注释不同的颜色

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

pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14))#在第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)

你可能感兴趣的:(初学者R语言:热图基础画法及个性化调整详解)