title: ggplot heat map with geom_tile using weight for fill color
body: <p>I'm trying to get a heat map using three variables: Discrete (x,y) for coordinates and a z which is a weight. I want bins/tiles to have colors depending on the sum. I get a decent result as follows:</p>
  
  <pre><code>x = c(8, 9, 10, 11, 12, 12)
  y = c(3, 4, 5, 6, 6, 6)
  z = c(1, 4, 2, 2, 8, 8)
  
  dd &lt;- data.frame(x,y,z)
  p &lt;- ggplot(dd, aes(x=x,y=y,weight=z)) + 
    stat_bin2d(bins=c(5,4))
  p
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/E3QZW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/E3QZW.png" alt="enter image description here"></a></p>
  
  <p>It's not exactly what I want, since the bins are squeezed into a space that's not really big enough and they're not centered onto the coordinates.</p>
  
  <p>I discovered <code>geom_tile</code> which seems to be the way to go, except I can't figure out how to get the <code>fill</code> color to be based on the weight (summed z for each bin) as it worked with <code>stat_bin2d</code>. The following uses a fill based on <code>z</code> but isn't the sum. You can see the difference in the value of the (12,6) bin which is 8 rather than 16.</p>
  
  <pre><code>    p &lt;- ggplot(dd, aes(x=x,y=y,weight=z)) + 
        geom_tile(aes(fill=z))  + 
        scale_fill_gradient(low = "black", high = "steelblue")
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/udME4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/udME4.png" alt="enter image description here"></a></p>
  
  <p>What am I missing?</p>