I am having issues with pheatmap legend annotations. I want to annotate my columns with metadata from my coldata (working with RNAseq, DESeq2).
DESeq2:
dds <- DESeqDataSetFromMatrix(countData = counts, # Raw expression counts.
colData = coldata, # Metadata with age & sex lables.
design = ~ age)
dds <- DESeq(dds, test="LRT", reduced = ~ 1) # LRT test for model fitting.
res <- results(dds)
vsd <- vst(dds, blind = FALSE)
plotPCA(vsd, intgroup = c("age","sex", "batch"), returnData = FALSE)
# Create annotation
heat_anno <- as.data.frame(colData(vsd)[,c("age", "sex")])
heat_colors <- list(
sex = c("M" = "#01bfc5", "F" = "salmon"))
Simplified version of the heatmap code:
pheatmap(mat = assay(vsd[genes, ]),
cluster_rows = TRUE,
cluster_cols = FALSE,
annotation_col = heat_anno,
labels_col = coldata$age)
The result looks like that:
The colors on top of the heatmap are correctly assigned to respective values. However, as you can see in my colnames, the legend for age incorrectly displays the min and max values as 2 and 8 instead of my 0.6 - 9.8. I want to set the legend annotation to 0 and 10 (or at the very least to min and max of my coldata$age). Does anybody know how I could do this? breaks and legend_breaks modifies the color gradient of the heatmap values themselves (here scaled expression), but not additional annotations.

Here is a version of the function
pheatmap:::draw_annotation_legendthat I have modified.Copy this function into your script (or into a
funct.rfile and then call the file with the commandsource("funct.r")).I have added three new parameters to this function:
nbreaks, which defines the number of breaks in the color gradient of the heatmap for the annotation legendtxt, which determines the labels to be placed at the extremes of the heatmap scale;txt="minmax"sets the labels at the minimum and maximum of the numeric variable (in your example, "age");txt=NULLuses the labels defined bygrid.pretty; you can also define two labels of your choice withtxt=c("lab1","lab2")cex.txt, which sets the magnification to be used for the labels.To minimize modifications to
pheatmap, I opted to have these three options defined directly in the defaults of the functiondraw_annotation_legend. In other words, if you want to modify these options, change their default values in the argument list of the function. It's not an elegant solution, but it's simple from a practical standpoint.Here is an example demonstrating the use of the modified function with data that I have generated.