title: Leaflet: Custom bin legend for raster
body: <p>I've been playing around with the <code>leaflet</code> package and can't create a proper legend for a raster.</p>
  
  <p>The problem is as follows: I've created an informative map with this code:</p>
  
  <p>raster file: <a href="https://drive.google.com/open?id=0B7mw858dxh5MODZqSHNHbFR3clU" rel="nofollow noreferrer">https://drive.google.com/open?id=0B7mw858dxh5MODZqSHNHbFR3clU</a></p>
  
  <pre><code>library(raster)
  library(leaflet)
  r &lt;- raster('raster.tif')
  colores &lt;- c('red', 'green', 'blue', 'chocolate', 'deeppink', 'grey')
  at &lt;- seq(0, 4800, 800)
  cb &lt;- colorBin(palette = colores, bins = length(at), domain = at)
  
  leaflet() %&gt;% 
      setView(-67.5,-16, zoom = 7) %&gt;%
      addRasterImage(r, colors = cb) %&gt;%
      addLegend(pal = cb, values = at)
  </code></pre>
  
  <p>Variable <code>r</code> is a raster with precipitation data ranging from 171 to 4667 mm/year.</p>
  
  <p>Output:</p>
  
  <p><a href="https://i.imgur.com/bt7Ew6O.png" rel="nofollow noreferrer">https://i.imgur.com/bt7Ew6O.png</a></p>
  
  <p>I want to get a legend with 6 bins using the colors passed to the variable <code>colores</code> but the output shows 10 bins ignoring the ranges I passed to the <code>at</code> variable.</p>
  
  <p>How should I achieve what I want?</p>