This is a test version of Biostars. For the public version, visit https://www.biostars.org.
problem changing color for annotations

Hi everyone,

I am doing a pheatmap with deseq2 data, and I'm not able to change the colors of the column annotations. I have 12 samples (columns) grouoped in 4 conditions, and I'm doing pair-wise comparisons like this, below. Now, my intention is change de default color of the group annotation to for example orange and skyblu (you can see in the script) but it seems doesn't work, but I don't get any error message. I hope someone can help me out :) Thanks!!

        > > sampleTable    
        sample  NamefileName    condition 
        1      1A 1A_gene_strict.tsv   WT_NS 
        2      2A 2A_gene_strict.tsv   WT_NS 
        3      3A 3A_gene_strict.tsv   WT_NS 
        4      1B 1B_gene_strict.tsv WT_ST
        5      2B 2B_gene_strict.tsv WT_ST
        6      3B 3B_gene_strict.tsv WT_ST
        7     6A 6A_gene_strict.tsv   KO_NS 
        8      7A 7A_gene_strict.tsv   KO_NS
         9     8A 8A_gene_strict.tsv   KO_NS 
        10    6B 6B_gene_strict.tsv KO_ST 
        11    7B 7B_gene_strict.tsv KO_ST
        12    8B 8B_gene_strict.tsv KO_ST

    levels <- unique(sampleTable$condition)
    ll <- length(levels)    

    for (i in 1:(ll-1)) {
            for (j in (i+1):ll) {
                       l1 <- toString(levels[i])
                l2 <- toString(levels[j])
                conditions = c(l1, l2)
                conds = subset(sampleTable, sampleTable$condition %in% conditions)
                samples = conds$sample
                df = data.frame(condition=conds$condition)
                rownames(df) = samples
                my_colour = list(df=c(l1="orange", l2="skyblue"))
                res <- results(dds, contrast=c("condition", l2, l1))
                res$FoldChange <- 2^res$log2FoldChange
                sig <- subset(res, res$padj < cutoff)
                subcounts <- subset(ncounts, rownames(ncounts) %in% rownames(sig))
                subcounts <- subcounts[,rownames(df)]
                lsubcounts <- log2(subcounts+1)
                pheatmap(mat=lsubcounts, color=hmcol, border_color=F, scale="row", cluster_cols=T,
                        cluster_rows=T, fontsize_row=9, annotation_col=df, annotation_colors=my_colour, 
                       show_rownames=F, show_colnames=T, annotation_names_col=F, annotation_names_row=F)
}
}
pheatmap r

Use the 101010 box to format the R code and use the Copy image address(Google Chrome) link for sharing images.

Done, thank you for the hint!

It's a bit hard to see within the loop structure, and without showing your error message, but I believe your problem may be that when you create the list to define your colors:

my_colour = list(df=c(l1="orange", l2="skyblue"))

The internal variable should be named like the column of the annotation that you are using (df) and want to color. So try changing it to this:

my_colour = list(condition=c(l1="orange", l2="skyblue"))

Thanks Papyrus. I've edited my post to understand better those levels. I tried to change my line by yours, but it is not working (substituting "df" by "condition" ), I have still the defaults colours... :(

Here the result of ´my_colour´

> my_colour
$conditions
CD4T_WT_NS CD4T_KO_NS 
    "grey"  "skyblue"

In that example it reads conditions, but it should be condition. Did you check that?

This example works for me:

counts <- matrix(1:8,4,2)
colnames(counts) <- c("l1","l2")
rownames(counts) <- c("a","b","c","d")
my_colour <- list(condition = c(l1 = "orange", l2 = "skyblue"))
df <- data.frame(condition = c("l1","l2"), row.names = c("l1","l2"))

pheatmap(counts, annotation_col = df, annotation_colors = my_colour)

With my_colour being:

> my_colour
$condition
       l1        l2 
 "orange" "skyblue"

Thank you for your reply! I solved the problem doing this: I defined my colors for the columns bar, and then, I created the list with ´condition´:

my_colour <- c("orange", "skyblue")
names(my_colour) <- c(l1, l2)
my_colour <- list(condition = my_colour)

and now it's working!

> my_colour
$condition
  CD4T_KO_NS CD4T_KO_STIM 
   "orange"    "skyblue"

Glad to know it worked out in the end!

Indeed, the names of the elements of the list (element my_colour$condition in this example) must match the columns of your annotation df (column df$condition). And the names of the vector of colors within each element in the list ( CD4T_KO_NS CD4T_KO_STIM in this example) must match the levels in the column of the annotation in df$condition.

If you had more than 1 column in the df, you could build a list with more elements and have multiple annotations!

0 answers

No answers yet.

Log in to answer this question.