title: How to create colorbars in ggplot similar to those created by Lattice
body: <p>I want colourbars created with ggplot to be similar to what spplot function (from lattice package) creates. Something like the attached image with each finite number of colours being assigned to rectangular blocks, instead of creating a continuous spectrum of colours. I need to be able to define the outline colour of the colourbar and also the format of the ticks.</p>
  
  <p><a href="https://i.stack.imgur.com/GLjSl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GLjSl.png" alt="An example of what I wish to have"></a></p>
  
  <p>I put this simple example together. How can I change this into something similar to this attached image? For example, I want the legend to start from -3 and end at 3 with 10 blocks of colours. I already tried 'nbin' in the function 'guides'. But I need the labels to be put at the 'edges' of the colour blocks instead of at the middle of them (i.e. centre of the bins).</p>
  
  <p>ps: And sometimes ggplot creates a labels beyond the length of the colourbar!</p>
  
  <pre><code>library(ggplot2)
  
  dat &lt;- data.frame(x = rnorm(100), y = rnorm(100), col=rnorm(100))
  
  ggplot(dat, aes(x,y,color=col)) + 
    geom_point() + 
    scale_color_gradient2(limits=c(-3,3), midpoint=0) +             
    guides(color=guide_colourbar(nbin=10, raster=FALSE))
  </code></pre>