title: R heatmap.2 manual grouping of rows and columns
body: <p>I have the following MWE in which I make a heatmap without performing any clustering and showing any dendrogram. I want to group my rows (genes) together in categories, in a better looking way than how it is now.</p>
  
  <p>This is the MWE:</p>
  
  <pre><code>#MWE
  library(gplots)
  mymat &lt;- matrix(rexp(600, rate=.1), ncol=12)
  colnames(mymat) &lt;- c(rep("treatment_1", 3), rep("treatment_2", 3), rep("treatment_3", 3), rep("treatment_4", 3))
  rownames(mymat) &lt;- paste("gene", 1:dim(mymat)[1], sep="_")
  rownames(mymat) &lt;- paste(rownames(mymat), c(rep("CATEGORY_1", 10), rep("CATEGORY_2", 10), rep("CATEGORY_3", 10), rep("CATEGORY_4", 10), rep("CATEGORY_5", 10)), sep=" --- ")
  mymat #50x12 MATRIX. 50 GENES IN 5 CATEGORIES, ACROSS 4 TREATMENTS WITH 3 REPLICATES EACH
  png(filename="TEST.png", height=800, width=600)
  print(
  heatmap.2(mymat, col=greenred(75),
        trace="none",
        keysize=1,
        margins=c(8,14),
        scale="row",
        dendrogram="none",
        Colv = FALSE,
        Rowv = FALSE,
        cexRow=0.5 + 1/log10(dim(mymat)[1]),
        cexCol=1.25,
        main="Genes grouped by categories")
  )
  dev.off()
  </code></pre>
  
  <p>Which produces this:</p>
  
  <p><a href="https://i.stack.imgur.com/WT3Fg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WT3Fg.png" alt="test1"></a></p>
  
  <p>I would like to group the CATEGORIES in the rows together (and, if possible, the treatments in the columns as well), so it looks something like the following:</p>
  
  <p><a href="https://i.stack.imgur.com/Thqc0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Thqc0.png" alt="TEST2"></a></p>
  
  <p>Or, maybe even better, with the CATEGORIES on the left, the same way as when clustering is performed and dendrograms are shown; however is easier and clearer...</p>
  
  <p>Is there any way? Thanks!!</p>
  
  <p><strong>EDIT!!</strong></p>
  
  <p><strong>I was made aware of the RowSideColors in the comments and I made the MWE below. However, I don't seem to be able to print the legend in the output png, plus the colors in the legend are not correct, and I cannot get the position right either. So please help me with the legend in the MWE below.</strong></p>
  
  <p><strong>On another hand, I use the palette "Set3", consisting of 12 colors, but what if I need more than 12 colors (if I have more than 12 categories)??</strong></p>
  
  <p><strong>NEW MWE</strong></p>
  
  <pre><code>library(gplots)
  library(RColorBrewer)
  col1 &lt;- brewer.pal(12, "Set3")
  mymat &lt;- matrix(rexp(600, rate=.1), ncol=12)
  colnames(mymat) &lt;- c(rep("treatment_1", 3), rep("treatment_2", 3), rep("treatment_3", 3), rep("treatment_4", 3))
  rownames(mymat) &lt;- paste("gene", 1:dim(mymat)[1], sep="_")
  mymat
  mydf &lt;- data.frame(gene=paste("gene", 1:dim(mymat)[1], sep="_"), category=c(rep("CATEGORY_1", 10), rep("CATEGORY_2", 10), rep("CATEGORY_3", 10), rep("CATEGORY_4", 10), rep("CATEGORY_5", 10)))
  mydf
  png(filename="TEST.png", height=800, width=600)
  print(
      heatmap.2(mymat, col=greenred(75),
                trace="none",
                keysize=1,
                margins=c(8,6),
                scale="row",
                dendrogram="none",
                Colv = FALSE,
                Rowv = FALSE,
                cexRow=0.5 + 1/log10(dim(mymat)[1]),
                cexCol=1.25,
                main="Genes grouped by categories",
                RowSideColors=col1[as.numeric(mydf$category)]
                )
      #THE LEGEND DOESN'T WORK INSIDE print(), AND THE POSITION AND COLORS ARE WRONG
      #legend("topright",
      #       legend = unique(mydf$category),
      #       col = col1[as.numeric(mydf$category)],
      #       lty= 1,
      #       lwd = 5,
      #       cex=.7
      #       )
  )
  dev.off()
  </code></pre>
  
  <p>Which produces:</p>
  
  <p><a href="https://i.stack.imgur.com/FnT2L.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FnT2L.png" alt="TEST3"></a></p>
  
  <p><strong>Please help me with the legend, and with the hypothetical case I need more than 12 colors. Thanks!</strong></p>