2

I wanted to have my conditions labelled on the heatmap I am making for DGE. This code:

mat <- assay(rld)[topVarGenes,]
condition = c("black", "orange")
names(condition) = c("Dark", "Light")
ann_colors = list(condition = condition)
pheatmap(mat, color = colorRampPalette(rev(brewer.pal(n = 7, name = "RdYlBu")))(24), annotation_colors = ann_colors[1], border_color = "grey60", fontsize = 12, scale = "row")

produces this heatmap: enter image description here

But, this heatmap doesn't have the conditions labelled above the columns like I wanted. So I tried this code:

annotation <- data.frame(annotation)
    pheatmap(mat, annotation = annotation, color = colorRampPalette(rev(brewer.pal(n = 7, name = "RdYlBu")))(24), border_color = "grey60", fontsize = 12, scale = "row")

Which almost works, but doesn't use the colors I want to label the conditions (samples 1-3 are "dark" condition and are to be labelled black and samples 4-6 are "light" condition and are to be labelled orange). This graph also includes a funky column label under condition for sample which is redundant and I don't know how to get rid of it. Also, the data.frame(annotation) is an excel sheet I imported of samples and corresponding conditions. enter image description here

Adding back the annotation_colors to the code:

 pheatmap(mat, annotation = annotation, color = colorRampPalette(rev(brewer.pal(n = 7, name = "RdYlBu")))(24), annotation_colors = ann_colors, border_color = "grey60", fontsize = 12, scale = "row")

produces this error: Error in convert_annotations(annotation_col, annotation_colors) : Factor levels on variable condition do not match with annotation_colors

Lastly, I tried this bit of code I found in a stack overflow post to define annotation, which gets R to use the correct colors, but they are not in the correct order for the conditions because the %% 2==0 causes it to label every other sample as 'light', but I can't think of anything else to do. Here is the code:

annotation <- data.frame(condition = factor(1:6 %% 2==0, labels = c("Dark", "Light")))

Help is greatly appreciated!

Dhawal Kapil
  • 2,584
  • 18
  • 31
rachelbasch
  • 21
  • 1
  • 2
  • Hey, Welcome to SO. I have modified your question to embed images as proper images. You can do that via the image button in the editor. Hope this helps – Dhawal Kapil Apr 02 '20 at 18:14
  • I had that same error message. My problem was that I was annotating clusters for columns and for rows. The variable name for the clusters was the same for columns and rows AND the number of clusters where different (I did not get the error message when the number of clusters was the same for rows and columns). The solution for me was simply change the name of one of the variable that stored the levels of the column clusters. – jorvaor Jul 06 '23 at 10:59

1 Answers1

4

It's not so clear in the vignette, but you can follow the steps below to generate the right data.frame and list, no reason not to work:

First I make a matrix like yours:

library(pheatmap)
M = cbind(matrix(runif(30,min=0,max=0.5),ncol=3),
matrix(runif(30,min=0.3,max=0.8),ncol=3))
rownames(M) = paste0("row",1:10)
colnames(M) = paste0("sample",1:6)

Let's say first 3 columns are "light", and last 3 columns are "dark". We create a data.frame for this, important thing is to have rownames that match the colnames of your matrix:

ann_column = data.frame(
condition = rep(c("light","dark"),each=3))
rownames(ann_col) = colnames(M)

ann_column
  condition
1     light
2     light
3     light
4      dark
5      dark
6      dark

Now for the colors, you need a list, and the names need to match the data frame above, and inside the light, you specify what factor matches what color, so:

ann_colors = list(condition = c(dark="black",light="orange"))

And we draw it:

pheatmap(M,annotation_col=ann_col,annotation_colors=ann_colors)

enter image description here

StupidWolf
  • 45,075
  • 17
  • 40
  • 72