title: R - Add a colored side bar to a heatmap.2
body: <p>I have several problems with R.
  I am trying to plot a heatmap with both row and col dendrograms, till here everything is okay. Now I want to add a side bar using ColSideColors. The colors depends of the proteins' domains, I have these data in a different vector called domains, containing the names of the proteins and the colors associated to the domains. I have already check, each domain has a color, everything is fine. 
  But when I add the side bar to the plot, it seems that the color are not in the correct order. How can I fix this ?
  Please find the code bellow :</p>
  
  <pre><code>domains &lt;- read.table("pubchem_proteins_domains.csv", sep = ",", dec  = ".", quote = "\"")
  domains$values  &lt;- (domains$V2 - min(domains$V2))/(max(domains$V2) - min(domains$V2))
  domains$gray &lt;- gray(domains$values)
  
  head(domains)
  
                                                                               V1
  1           P10275.ANDR_HUMAN.Androgen.receptor.OS.Homo.sapiens.GN.AR.PE.1.SV.2
  2       P00915.CAH1_HUMAN.Carbonic.anhydrase.1.OS.Homo.sapiens.GN.CA1.PE.1.SV.2
  3       P00918.CAH2_HUMAN.Carbonic.anhydrase.2.OS.Homo.sapiens.GN.CA2.PE.1.SV.2
  4       P22748.CAH4_HUMAN.Carbonic.anhydrase.4.OS.Homo.sapiens.GN.CA4.PE.1.SV.2
  5    P21554.CNR1_HUMAN.Cannabinoid.receptor.1.OS.Homo.sapiens.GN.CNR1.PE.1.SV.1
  6 P24941.CDK2_HUMAN.Cyclin-dependent.kinase.2.OS.Homo.sapiens.GN.CDK2.PE.1.SV.2
    V2 values    gray
  1 16   0.60 #999999
  2  7   0.24 #3D3D3D
  3  7   0.24 #3D3D3D
  4  7   0.24 #3D3D3D
  5  2   0.04 #0A0A0A
  6 22   0.84 #D6D6D6
  
  mol_distances &lt;- read.table("pubchem_molecular_distances.csv", header = T, sep = ",")
  row.names(mol_distances) &lt;- mol_distances[,1]
  mol_distances &lt;- mol_distances[,-c(1)]
  
  alignment &lt;- read.alignment(file = "pubchem_alignment.txt", format = "clustal")
  prot_distances &lt;- dist.alignment(alignment)
  
  hc_mol &lt;- hclust(as.dist(mol_distances), method="ward")
  hc_prot &lt;- hclust(as.dist(prot_distances), method="ward")
  
  heatmap.2(dat_mat, 
  Rowv = as.dendrogram(hc_prot), 
  Colv = as.dendrogram(hc_mol), 
  RowSideColors=domains$gray[domains$proteins_name],
  dendrogram = "both",
  scale = "none",
  margins = c(12,24),
  key = TRUE,
  keysize = 1.0,
  col = rainbow(512, start = 1, end = 0.4), 
  density.info="density", 
  denscol = "black",
  xlab="Compounds", 
  ylab="Targets", main="X Activity Profile",
  trace ="none")
  </code></pre>
  
  <p>Beside, I could not find another function to do similar things in R for colors, can i do the same with rainbow ? </p>
  
  <p>Thank you
  Regards
  Deuterium</p>