title: Ggplot2: Heatmap + Bar chart combination and tweaking
body: <p>I'm new to R and am trying to visualise data that is broken down by company and by year for a project at university. I want to try and add bars (as in bar chart bars) to the top and right hand side of the heatmap I've created below, to provide a direct method of comparing count data between the companies (right y axis), and between years for the group as a whole (top x axis). </p>
  
  <p>I know I could probably merge images in Illustrator or something pre-publication, but I know the software is capable of adding together graphics (in a Lattice i think?) and I would like to improve my skills with R and ggplot.</p>
  
  <p>Ideally I would also like to learn to: </p>
  
  <p>1) add the values of each tile superimposed on top</p>
  
  <pre><code>geom_text(aes(fill = trialx.m$value, label = trialx.m$value)
  </code></pre>
  
  <p>doesn't seem to work for me?</p>
  
  <p>2) Move the legend so that it was out of the way of the bar plots</p>
  
  <p>3) Adjust the legend size and scale (to account for the data scaling)</p>
  
  <p>4) order the heatmap vertically with the bar sizes</p>
  
  <p>I know this is a lot, but I would appreciate help or advice about any part.</p>
  
  <p>What I'm currently doing:</p>
  
  <p><strong>Re-scaling</strong></p>
  
  <pre><code>library(ggplot2)
  trialx.m &lt;- melt(trialx)
  trialx.m &lt;- ddply(trialx.m, .(variable), transform, rescale = scale(value))
  </code></pre>
  
  <p><strong>Plotting</strong></p>
  
  <pre><code>(p &lt;- ggplot(trialx.m, aes(variable, Company)) + 
      geom_tile(aes(fill = rescale), colour = "white") + 
      scale_fill_gradient(low = "ghostwhite", high = "darkblue"))
  </code></pre>
  
  <p><strong>Neaten, remove background, rotate text etc.</strong></p>
  
  <pre><code>p + theme_grey(base_size = base_size) + labs(x = "", y = "") + 
      scale_x_discrete(expand = c(0, 0)) + scale_y_discrete(expand = c(0, 0)) +
      opts(legend.position = "", axis.ticks = theme_blank(), 
          axis.text.x = theme_text(size = base_size * 0.8, angle = 90, hjust = 0, 
          colour = "grey50"))
  </code></pre>
  
  <p><a href="https://www.dropbox.com/s/2ixjkifddu9gnnj/stackoverflowsample.txt" rel="nofollow">Here is a dropbox link</a> to the data I am using: </p>