title: How can I individualize the order of rows in heatmap.2 (gplots)?
body: <p>I am interested in visualizing the distribution of my outcome variable using <code>heatmap.2</code>, as provided in the <code>gplots</code> package.</p>
  
  <p>This is what my <em>dataset</em> looks like:</p>
  
  <pre><code>&gt; head(data.w)
  year Armenia Azerbaijan Bulgaria Croatia Cyprus Czech Republic Estonia Georgia 
  1998       0          0     1.14       0   1.21           1.32    1.43       0    
  1999       0          0     1.32       0   1.44           1.50    1.68       0  
  2000       0          0     1.32       0   1.63           1.59    1.75       0  
  2001       0.75       0     1.46       0   1.85           1.93    1.81       0  
  2002       0.82       0     1.74       0   2.04           2.00    2.00       0.34  
  2003       0.87       0     1.85       0   2.22           2.22    2.19       0.42
  </code></pre>
  
  <p>To draw the <strong>heatmap</strong>, I use the following <em>code</em>:</p>
  
  <pre><code>library(gplots) 
  library(grDevices)
  
  min(data.w[,2:9]) 
  max(data.w[,2:9]) 
  data.wx &lt;-as.matrix(data.w)
  
  pdf("heatmap.pdf", width=10,height=5)
  heatmap.2(as.matrix(t(data.w[,2:9])), trace="none", 
            Rowv=NULL, Colv=NULL, dendrogram="none",
            labCol=data.w$year,labRow=data.w$cname,
            lhei=c(0.1,1), lwid=c(0.1,1), margins=c(5,10),
            colsep=1:16, rowsep=1:9, sepcolor="white",
            col=gray.colors(8, start=1, end=0) ) 
  dev.off()
  </code></pre>
  
  <p>Overall, I am rather happy with what the plot looks like:</p>
  
  <p><a href="https://i.stack.imgur.com/w9gtq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/w9gtq.png" alt="enter image description here"></a> </p>
  
  <p>Yet, the countries displayed actually belong to <strong>two different groups</strong>, with Armenia, Azerbaijan, and Georgia belonging to the group "neighbourhood" and the remaining countries being members of the group "enlargement". </p>
  
  <p>In short, <em>I would like to split the heatmap by group membership.</em> Within each group, countries should not be listed alphabetically or according to values but I would need to individualize the order according to some specific criteria.</p>
  
  <p>Importantly, I do not want to </p>
  
  <ul>
  <li>draw two separate plots as this would change the distribution of
  colours;  </li>
  <li>group the row variable by using <code>RowSideColors</code> as
  additional argument, as for instance suggested <a href="http://sebastianraschka.com/Articles/heatmaps_in_r.html" rel="nofollow noreferrer">here</a> in order to
  categorize the input variables.</li>
  </ul>
  
  <p>Rather, the final heatmap should be organized as follows:
  <a href="https://i.stack.imgur.com/bFB1p.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bFB1p.png" alt="enter image description here"></a></p>
  
  <p><em>What would I need to add to the plot in order to get such heatmap.2 in which the order of rows is individualized?
  Please note that extra space between the rows separating the groups is not needed.</em> </p>
  
  <p>Thanks a million!</p>