ColSideColors' must be a character vector of length ncol(x)
1
1
Entering edit mode
2.2 years ago
Jakpa ▴ 50

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

<code>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:

<code>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"

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


where, am I getting it wrong?

Regards, Anthony

R heatmap.2 differential gene expression • 5.4k views
0
Entering edit mode

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

1
Entering edit mode
2.2 years ago

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

0
Entering edit mode

Hi Kelvin, 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 
0
Entering edit mode

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

0
Entering edit mode

Hello Kelvin,

I hope the re-formatted post is ok. am still learning learning how to write codes in this Platform.

Regards

0
Entering edit mode

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.

1
Entering edit mode

Hi Kelvin, I saw the error. It was from sample_dist

 I tranposed the matrix like this: <code>sample_dist<- as.matrix(dist(t(assay(rlog_Trans)))) The heatMap is ok now with both groups Thanks `