This is a test version of Biostars. For the public version, visit https://www.biostars.org.
ColSideColors' must be a character vector of length ncol(x)

Hello all,

I tried to generate a heatmap using heatmap.2. The following code was used:

heatmap.2(
  as.matrix(sample_dist),
  key=F,
  trace="none",
  Colv = c("cancer", "normal"),
  Rowv ="Colv",
  dendrogram= "none",
  col=colorpanel(100, "black", "white"),
  ColSideColors=myColors[condn],
  RowSideColors=myColors[condn],
  margin=c(10, 10),
  main="Sample Distance Matrix")

Then, I got this error:

Error in heatmap.2(as.matrix(sample_dist), key = F, trace = "none", Colv = c("cancer", : 'ColSideColors' must be a character vector of length ncol(x)

Does this mean that myColors is not same as condn?

This was how I defined myColors and condn

myColors<- brewer.pal(8, "Set3")[1:length(unique(group))]
condn<- factor(c(rep("normal",3), rep("cancer",3)))

Where am I getting it wrong?

Regards,
Anthony

r heatmap.2 differential-gene-expression

1 answer

It is likely that, when you solve this first error, a subsequent error will appear relating to RowSideColors.

The only thing that you need to pass to ColSideColors and RowSideColors are character vectors of colours that have the same length as ncol(x) and nrow(x), respectively.

  • What, currently, is contained in myColours?
  • What is group?
  • What are you aiming to do with this: myColors[condn]?

At the very fundamental level, we can assign a pre-defined colour palette like this:

samples <- c('Primary','Primary','Normal','Met','Normal','Normal','Met','Met','Primary','Normal')
samples <- factor(samples, levels = c('Normal', 'Primary', 'Met'))
RColorBrewer::brewer.pal(11, 'Spectral')[samples]
 [1] "#D53E4F" "#D53E4F" "#9E0142" "#F46D43" "#9E0142" "#9E0142" "#F46D43" "#F46D43" "#D53E4F" "#9E0142"

Other ways to assign colours to factors:

colorRampPalette(c('pink', 'purple', 'red4'))(length(unique(samples)))[samples]
 [1] "#A020F0" "#A020F0" "#FFC0CB" "#8B0000" "#FFC0CB" "#FFC0CB" "#8B0000" "#8B0000" "#A020F0"
[10] "#FFC0CB"

...or even just:

c('pink', 'purple', 'red4')[samples]
 [1] "purple" "purple" "pink"   "red4"   "pink"   "pink"   "red4"   "red4"   "purple" "pink"

Gracias!

Kevin

Hi Kevin,

Thank you for your response.

This is what is in myColors:

 [1] "#8DD3C7" "#FFFFB3"

For group I have this:

factor  w/2 levels "HBR; "UHR" : 1 1 1 2 2 2>`

condn:

factor w/2 levels "cancer", "normal" : 2 2 2 1 1 1

The group and condn are what am using for my comparison in my Differential gene Expression. From your examples, I think I have few numbers of colors compare to what i have in group and condn. How do I make this up?

Am relatively new with R

Thank you,
Anthony

Can you try to re-format your post? - thanks!

Hello Kevin,

I hope the re-formatted post is ok. I am still learning learning how to write code on this platform.

Regards

You have 2 colours and 2 groups? If that is the case, then this should work

myColors[condn]

...or:

myColors[group]

For RowSideColors, it is a different problem - I recommend that you avoid using that, for now.

Hi Kevin,

I saw the error. It was from sample_dist

I transposed the matrix like this:

sample_dist<- as.matrix(dist(t(assay(rlog_Trans))))

The heatMap is ok now with both groups

Thanks

Hello Kelvin, pardon my formatting style. I am still learning it

Log in to answer this question.