CITE网址https://www.jianshu.com/p/398115d2d2e8
本文将绘制静态与交互式热图,需要使用到以下R包和函数:
heatmap()
:用于绘制简单热图的函数
heatmap.2()
:绘制增强热图的函数
d3heatmap
:用于绘制交互式热图的R包
ComplexHeatmap
:用于绘制、注释和排列复杂热图的R&bioconductor
包(非常适用于基因组数据分析)
使用基本函数绘制简单热图
主要是函数heatmap(x, scale="row",col=)
- x:数据矩阵
- scale:表示不同方向,可选值有:row, column, none
- col:颜色
- column_title:列标题
- row_title:行标题
##自定义颜色,设置行列标题
mycol <- colorRamp2(c(-2, 0, 2), c("blue", "white", "red"))
Heatmap(df, name = "mtcars", col = mycol,
column_title = "Column title",
row_title = "Row title")
注意,行标题的默认位置是“left”,列标题的默认是“top”。可以使用以下选项更改:
-
row_title_side
:允许的值为“左”或“右”(例如:row_title_side =“right”) -
column_title_side
:允许的值为“top”或“bottom”(例如:column_title_side =“bottom”) 也可以使用以下选项修改字体和大小: -
row_title_gp
:用于绘制行文本的图形参数 -
column_title_gp
:用于绘制列文本的图形参数
fontface
的可能值可以是整数或字符串:1 = plain,2 = bold,3 =斜体,4 =粗体斜体。如果是字符串,则有效值为:“plain”
,“bold”
,“italic”
,“oblique”
和“bold.italic”
。
##对行列标题的字体大小形式进行调整
Heatmap(df, name = "mtcars", col = mycol,
column_title = "Column title",
column_title_gp = gpar(fontsize = 14, fontface = "bold"),
row_title = "Row title",
row_title_gp = gpar(fontsize = 14, fontface = "bold"))
显示行/列名称:
-
show_row_names
:是否显示行名称。默认值为TRUE
-
show_column_names
:是否显示列名称。默认值为TRUE
##不显示行名称
Heatmap(df, name = "mtcars", show_row_names = FALSE)
更改聚类外观
默认情况下,行和列是包含在聚类里的。可以使用参数修改:
-
cluster_rows = FALSE
。如果为TRUE
,则在行上创建集群 -
cluster_columns = FALSE
。如果为TRUE
,则将列置于簇上
# 不对列进行聚类
Heatmap(df, name = "mtcars", col = mycol, cluster_rows = FALSE)
对列和行的高度进行调整
treeheight_row
:参数设定行的高度
treeheight_col
:参数设定列聚类树的高度
# treeheight_row和treeheight_col参数设定行和列聚类树的高度,默认为50
pheatmap(test, treeheight_row = 30, treeheight_col = 50)
设定cell 的大小
cellwidth
:宽度
cellheight
:高度
##宽度为15,高为12,字体大小为10
pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 10)
在热图中展现数值
通过设定text来实现
display_numbers = TRUE
:参数设定在每个热图格子中显示相应的数值
number_color
:参数设置数值字体的颜色
number_format
:设定数值的显示格式
display_numbers
:设定条件式展示
pheatmap(test, display_numbers = matrix(ifelse(test> 5,"*",""), nrow(test)))
##在每个格子中显示数值,设定颜色为蓝色
pheatmap(test, display_numbers = TRUE,number_color ="blue")
# 数值展示形式为小数点后一位的自然数表示方法
pheatmap(test, display_numbers = TRUE, number_format ="%.1e")
#把数值大于5的用*表示
pheatmap(test, display_numbers = matrix(ifelse(test> 5,"*",""), nrow(test)))
设置图列
设定legend展示的值
legend_breaks:参数设定图例显示范围
legend_label:参数添加图例标签
#设定图例显示范围为-1到4,图例标签对应参数为0,。。。。
pheatmap(test, cluster_row = FALSE, legend_breaks = -1:4, legend_labels = c("0","1e-4","1e-3","1e-2","1e-1","1"))
#去掉legend
pheatmap(test, legend = FALSE)
自定义颜色
``color = colorRampPalette()`
border_color:设定每个热图格子的边框色
border:是否要边框线
#colorRampPalette
pheatmap(test, color = colorRampPalette(c("navy","white","firebrick3"))(50))
pheatmap(test, border_color ="red", border=TRUE)
注释annotations
annotation_col:列的注释
annotation_row:行的注释
annotation_color:注释的颜色
angle_col:改变列标签的角度
###生成行、列的注释
##生成列的注释data.frame
annotation_col=data.frame(CellType = factor(rep(c("CT1","CT2"), 5)), Time = 1:5 )
##对列的注释表的行名重新命名
rownames(annotation_col) = paste("Test", 1:10, sep ="")
##生成行的注释data.frame
annotation_row = data.frame( GeneClass = factor(rep(c("Path1","Path2","Path3"), c(10, 4, 6))))
##行的注释的行名重新命名
rownames(annotation_row) = paste("Gene", 1:20, sep ="")
##添加行、列的注释
pheatmap(test, annotation_col = annotation_col,
annotation_row = annotation_row)
##改变列标签的角度为45°
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row,
angle_col ="45")
##自定义注释分组及颜色
ann_colors = list(Time=c("white","firebrick"),
CellType=c(CT1="#1B9E77",CT2="#D95F02"), GeneClass=c(Path1="#7570B3",Path2="#E7298A",Path3="#66A61E") )
##按照自定义颜色给annotation上色,标题为Title
pheatmap(test, annotation_col = annotation_col,
annotation_row=annotation_row,
annotation_colors = ann_colors,
main ="Title")
设定gap
gaps_row
:可以根据行的聚类数将热图分隔开,注意使用时行不能聚类
cutree_col
:可以根据列的聚类数将热图分隔开
#根据行聚类结果,设定行gap,分3部分,行不聚类
pheatmap(test, annotation_col = annotation_col,
cluster_rows = FALSE,
gaps_row = c(10, 14))
#根据列聚类结果,设定列gap
pheatmap(test,annotation_col = annotation_col,
cluster_cols = T,
cutree_col = 2)
未完待续