This question was also asked on github

I'm trying to run the function HeatmapAnnotation (a function in the ComplexHeatmap package). When I run it, I get the error "Error in col2rgb(colors, alpha = TRUE) : invalid RGB specification"

col2rgb() is not among the libraries I've loaded and it doesn't seem to be a method contained in HeatmapAnnotation.

Here's the code block for the section:

df <- data.frame(labid= combmetadata$Sample_Name,
             Diagnosis= combmetadata$Diagnosis,
             Tissue = combmetadata$Tissue,
             Tumor.Normal = combmetadata$T.N,
             study = combmetadata$batch)

 labid <- colnames(bValsselect)
 df <- left_join(as.data.frame(labid),df,by="labid")
 df <- df[,c(2,4,5)]
 df$study[df$study == "1" | df$study == "2"] <- "cgid"

 df$Diagnosis <- factor(df$Diagnosis, levels = c("Uterine endometrial","Ovarian endometrioid",
                                            "Ovarian mucinous",
                                            "Colorectal mucinous",
                                            "Pancreas mucinous",
                                            "Stomach mucinous"))

 ha_column = HeatmapAnnotation(df = df, 
                          col = list(Diagnosis = c("Uterine endometrial" = "#DDCC77F",
                                          "Ovarian endometrioid" = "#0F7554",
                                          "Ovarian mucinous" = "#44AA99",
                                          "Colorectal mucinous" = "#882255",
                                          "Pancreas mucinous" = "#AA4499",
                                          "Stomach mucinous" = "#D695D0"),
                            Tumor.Normal = c("N" = "grey",
                                             "T" = "black"),
                            study = c("cgid" = "#332288",
                                      "tcga" = "#BD9AF7")))

hegt <- Heatmap(bValsselect,
            name = "Beta Value",
            col = colorRamp2(c(0,0.25,0.5),c("#00FFCC","#FFFFFF","#0099FF")),
            show_row_names = FALSE, 
            top_annotation = ha_column,
            show_row_dend = F)

The code runs fine up until it reaches the end of the HeatmapAnnotation() call

I've tried to load col2rgb() and override the alpha setting. Can anyone give me a clue about what's happening and any suggestions on how to correct the problem? I'm running R 4.2. Thanks


2 Answers 2


Looking at the documentation, it seems that HeatmapAnnotation expects a vector, whereas you are giving it a list. It is not clear on what acceptable color types are. It might even work with a list, but I'd suggest playing around with what you are supplying as your col parameter.

Note that HeatmapAnnotation has to do color mapping of some kind, the code is a little dense, but it does look like it calls out to SingleAnnotation, which in turn runs ColorRamp2, which does use col2rgb.

It also looks like col2rgb is called in ColorRamp2, which you use in the Heatmap call. How sure are you that your script isn't making it that far? It would help to see error output, possibly.

That said, when I take your isolated ColorRamp2 and run it, it goes ok. Possibly pull out the ColorRamp2 call and run it yourself in your environment to make sure nothing weird is happening.

Try moving the assignment of the colors outside the Heatmap call, occasionally supplying function calls as parameters like that causes issues.

  • 2
    $\begingroup$ You're right about the chain of calls. I do know that the code didn't run past the HeatmapAnnotation() function because in the process of debugging it, I could generate the error before I run the Heatmap() subblock. However, I just realized that the problem is that the first color assignment contains 7 alphanumeric characters instead of 6. That's what was causing the function to fail. Thanks for your help though. It certainly helps me understand the function a little better. $\endgroup$
    – Lou_A
    May 2, 2022 at 19:30

The alphanumeric value for the first color assignment contains 7 alphanumeric characters instead of 6. Correcting this typo fixed the problem.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.