⛳
【Plot】pheatmapにlegend titleをつける
pheatmapパッケージでヒートマップを描く場合、カラーマップ(値の凡例)のタイトルをつけるオプションが無い。
demo data
df <- iris[,1:4]
anno <- iris[,5, drop=FALSE]
rownames(df) <- rownames(anno) <- as.character(seq(nrow(df)))
pheatmap::pheatmap(mat = df, annotation_row = anno, show_rownames = F)

ComplexHeatmap
ComplexHeatmapパッケージにもpheatmap()機能があり、pheatmapパッケージと同様のヒートマップを描くことができる。ComplexHeatmapのpheatmapであれば、name=引数でlegend titleを指定することができる。
name引数を使用
ComplexHeatmap::pheatmap(
mat = df,
annotation_row = anno,
show_rownames = F,
name = "Score")

heatmap_legend_param =引数を使う方法もある。こちらはtitle名以外にも様々なlegend styleの調整が行える。
heatmap_legend_param引数を使用
ComplexHeatmap::pheatmap(
mat = df,
annotation_row = anno,
show_rownames = F,
heatmap_legend_param = list(title = "Score",
title_position = "leftcenter-rot",
labels = c("Low", "Median", "High"),
at = c(0,4,8),
legend_height = grid::unit(30,"mm"),
grid_width = grid::unit(3,"mm"),
labels_gp = gpar(col = "blue", font = 4),
border = "black")
)

詳しくはこちらを参照
https://jokergoo.github.io/ComplexHeatmap-reference/book/legends.html
legend position
pheatmap::pheatmap()にはlegendの配置を変更する機能が無い。ComplexHeatmap::pheatmap()の場合は、plotオブジェクトを作って、draw()機能内のheatmap_legend_side = オプションから変更可能。
次の例ではパイプでplotデータをdraw()に渡している。
library(dplyr)
ComplexHeatmap::pheatmap(
mat = df,
annotation_row = anno,
show_rownames = F,
heatmap_legend_param = list(title = "Score",
title_position = "topcenter",
legend_direction = "horizontal"
)
) %>% ComplexHeatmap::draw(heatmap_legend_side = "bottom")

ComplexHeatmap::Heatmap()であれば、draw()のannotation_legend_side=オプションとheatmap_legend_size=オプションでそれぞれのlegendの位置を指定できるが、ComplexHeatmap::pheatmap()ではannotation_legend_side=オプションが効かなかった。
Discussion