title: Raster map with discrete color scale for negative and positive values R
body: <p>I have two dataframes which I will like to map. The dfs have the same xy coordinates and I need a <strong>single colorbar</strong> with a visible <strong>discrete color scale</strong> for both dfs like the one shown here. I would like the colors in the colorkey to match the self-defined breaks. a more general solution that can be applied outside this example is much appreciated</p>
  
  <p><a href="https://i.stack.imgur.com/Sf6cw.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/Sf6cw.jpg" alt="enter image description here"></a></p>
  
  <p>The RdYIBu color palette from the <strong>RcolorBrewer</strong> package is what I am after.</p>
  
  <p><a href="https://i.stack.imgur.com/q3N7v.png" rel="noreferrer"><img src="https://i.stack.imgur.com/q3N7v.png" alt="enter image description here"></a></p>
  
  <p>My code so far:</p>
  
  <pre><code>library(rasterVis)
  ras1 &lt;- raster(nrow=10,ncol=10) 
  set.seed(1) 
  ras1[] &lt;- rchisq(df=10,n=10*10) 
  ras2=ras1*(-1)/2 
  s &lt;- stack(ras1,ras2) 
  Uniques &lt;- cellStats(s,stat=unique) 
  Uniques.max &lt;- max(Uniques)
  Uniques.min &lt;- min(Uniques)
  my.at &lt;- round(seq(ceiling(Uniques.max), floor(Uniques.min), length.out= 10),0)
  myColorkey &lt;- list(at=my.at, labels=list(at=my.at)) 
  levelplot(s, at=my.at, colorkey=myColorkey,par.settings=RdBuTheme())
  </code></pre>
  
  <p>How can I set the values in the colorkey to match values on the map as shown on the sample map above? Note that the number of colors in the colorkey should be the same number shown on the map.</p>
  
  <p>Many thanks for your help. Your suggestions will help me to develop many such maps.</p>
  
  <p>Thanks.</p>