title: ggplot2 visualizing counts of points plotted on top of each other: stat_bin2d or geom_tile or point size?
body: <p>My problem is simple: I have some points with x,y coordinates, which are positioned inside a rectangular grid made up of 1x1 squares. These points have averaged coordinates, so that several points are given the same coordinates (they overlap completely). Reproducible example:</p>
  
  <pre><code># generate fake data
  y &lt;- seq(from=0.5, to=9.5, by=1)
  x &lt;- seq(from=0.5, to=4.5, by=1)
  xnew &lt;- sample(x,100,replace=T)
  ynew &lt;- sample(y,100,replace=T)
  data &lt;- data.frame(xnew,ynew)
  
  # create chart
  ggplot(data, aes(x=xnew, y=ynew)) + geom_point()
  </code></pre>
  
  <p>I want to represent the frequency of points at a particular location (x,y coordinate, representing a particular square). <code>stat_bin2d</code> is a step in the right direction, but the bins are inexplicably (for me) placed at different spots on the map, making it difficult to see the distribution visually. </p>
  
  <p>I can imagine two different solutions:</p>
  
  <p>1) Is there a way to center the bins on the points? Sometimes the lower left-hand corner is at the point, sometimes the lower right-hand corner, etc. like this: <img src="https://i.stack.imgur.com/B2reI.png" alt="as in here"></p>
  
  <p>Additionally, it would be ideal if the boxes would be big enough to touch each other, but when I change the <code>binwidth=c(1,1)</code> in <code>stat_bin2d()</code>, it actually changes the counts, although the bins should not overlap, since all points are at least 1 square away from each other.</p>
  
  <p>or, using point sizes: </p>
  
  <p>2) I would prefer points whose size would reflect the concentration (that way would be better in black and white, too). I tried with geom_point():</p>
  
  <pre><code>ggplot(data, aes(x=xnew, y=ynew))+geom_point(aes(x=xnew,y=ynew, size=..count..))
  </code></pre>
  
  <p>but I got</p>
  
  <pre><code>Error in eval(expr, envir, enclos) : object 'count' not found
  </code></pre>
  
  <p>and then, if I add `stat="bin", it conflicts with the assignment to y. I looked here:<a href="https://stackoverflow.com/questions/3190087/why-wont-ggplot2-allow-me-to-set-a-size-for-each-individual-point">Why wont ggplot2 allow me to set a size for each individual point?</a>, but was not able to make it work. </p>
  
  <p>Thanks in anticipation for any help. </p>