title: Geographical heat map of a custom property in R with ggmap
body: <p>The goal is to build something like 
  <a href="http://rentheatmap.com/sanfrancisco.html" rel="nofollow noreferrer">http://rentheatmap.com/sanfrancisco.html</a></p>
  
  <p>I got map with ggmap and able to plot points on top of it. </p>
  
  <pre><code>library('ggmap')
  map &lt;- get_map(location=c(lon=20.46667, lat=44.81667), zoom=12, maptype='roadmap', color='bw')
  positions &lt;- data.frame(lon=rnorm(100, mean=20.46667, sd=0.05), lat=rnorm(100, mean=44.81667, sd=0.05), price=rnorm(10, mean=1000, sd=300))
  ggmap(map) + geom_point(data=positions, mapping=aes(lon, lat)) + stat_density2d(data=positions, mapping=aes(x=lon, y=lat, fill=..level..), geom="polygon", alpha=0.3)
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/0lcmk.png" alt="stat_density2d on a map"></p>
  
  <p>This is a nice image based on density. Does anybody know how to make something that looks the same, but uses position$property to build contours and scale?</p>
  
  <p>I looked thoroughly through stackoverflow.com and did not find a solution.</p>
  
  <p><strong>EDIT 1</strong></p>
  
  <pre><code>positions$price_cuts &lt;- cut(positions$price, breaks=5)
  ggmap(map) + stat_density2d(data=positions, mapping=aes(x=lon, y=lat, fill=price_cuts), alpha=0.3, geom="polygon")
  </code></pre>
  
  <p>Results in five independent stat_density plots:
  <img src="https://i.stack.imgur.com/biicn.png" alt="enter image description here"></p>
  
  <p><strong>EDIT 2 (from <a href="https://stackoverflow.com/users/1457051/hrbrmstr">hrbrmstr</a>)</strong></p>
  
  <pre><code>positions &lt;- data.frame(lon=rnorm(10000, mean=20.46667, sd=0.05), lat=rnorm(10000, mean=44.81667, sd=0.05), price=rnorm(10, mean=1000, sd=300))
  positions$price &lt;- ((20.46667 - positions$lon) ^ 2 + (44.81667 - positions$lat) ^ 2) ^ 0.5 * 10000
  positions &lt;- data.frame(lon=rnorm(10000, mean=20.46667, sd=0.05), lat=rnorm(10000, mean=44.81667, sd=0.05))
  positions$price &lt;- ((20.46667 - positions$lon) ^ 2 + (44.81667 - positions$lat) ^ 2) ^ 0.5 * 10000
  positions &lt;- subset(positions, price &lt; 1000)
  positions$price_cuts &lt;- cut(positions$price, breaks=5)
  ggmap(map) + geom_hex(data=positions, aes(fill=price_cuts), alpha=0.3)
  </code></pre>
  
  <p>Results in:
  <img src="https://i.stack.imgur.com/hD6if.png" alt="enter image description here"></p>
  
  <p>It creates a decent picture on real data as well. This is the best result so far. More suggestions are welcome.</p>
  
  <p><strong>EDIT 3:</strong>
  Here is test data and results of a method above:</p>
  
  <p><a href="https://raw.githubusercontent.com/artem-fedosov/share/master/kernel_smoothing_ggplot.csv" rel="nofollow noreferrer">https://raw.githubusercontent.com/artem-fedosov/share/master/kernel_smoothing_ggplot.csv</a></p>
  
  <pre><code>test&lt;-read.csv('test.csv')
  ggplot(data=test, aes(lon, lat, fill=price_cuts)) + stat_bin2d(, alpha=0.7) + geom_point() + scale_fill_brewer(palette="Blues")
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/Rpy0e.png" alt="enter image description here"></p>
  
  <p>I believe that there should some method that uses other than density kernel to compute proper polygons. It seems that the feature should be in ggplot out of the box, but I cannot find it.</p>
  
  <p>EDIT 4:
  I appreciate you time and effort to figure out the proper solution to this seemingly not too complicated question. I voted up both your answers as a good approximations to the goal.</p>
  
  <p>I revealed one problem: the data with circles are too artificial and the approaches do not perform that well on read world data. </p>
  
  <p>Paul's approach gave me the plot:
  <img src="https://i.stack.imgur.com/dyvPO.png" alt="enter image description here"></p>
  
  <p>It seems that it captures patterns of the data that is cool. </p>
  
  <p>jazzurro's approage gave me this plot:
  <img src="https://i.stack.imgur.com/WZGrC.png" alt="enter image description here"></p>
  
  <p>It got the patterns as well. However, both of the plots does not seem to be as beautiful as default stat_density2d plot. I will still wait a couple of days to look if some other solution will come up. If not, I will award the bounty to jazzurro as this will be the result I'll stick to use.</p>
  
  <p>There is an open python + google_maps version of required code. May be someone will find inspiration here:
  <a href="https://github.com/jeffkaufman/apartment_prices" rel="nofollow noreferrer">https://github.com/jeffkaufman/apartment_prices</a></p>