title: standardizing color scale, persp() plots in panel, R
body: <p>I'm creating a panel with multiple persp plots, each showing a range of data generated by tweaking predictor variables in a complex regression model. I want my 3D surface to be color coded by my z variable, which I was able to do easily enough. How do I take this color scale and make it cover the range of all z variable values that would be represented by all plots in my panel, so that the color scale between multiple persp plots can be compared? </p>
  
  <pre><code>#colorized z scale, only a smidge modified from http://www.inside-r.org/r-doc/graphics/persp
  par(bg = "white")
  par(mfrow=c(1,2))
  x &lt;- seq(-1.95, 1.95, length = 30)
  y &lt;- seq(-1.95, 1.95, length = 35)
  z1 &lt;- outer(x, y, function(a, b) a*b^2)
  nrz &lt;- nrow(z1)
  ncz &lt;- ncol(z1)
  # Create a function interpolating colors in the range of specified colors
  jet.colors &lt;- colorRampPalette( c("blue", "green") )
  # Generate the desired number of colors from this palette
  nbcol &lt;- 100
  color &lt;- jet.colors(nbcol)
  # Compute the z-value at the facet centres
  zfacet &lt;- z1[-1, -1] + z1[-1, -ncz] + z1[-nrz, -1] + z1[-nrz, -ncz]
  # Recode facet z-values into color indices
  facetcol &lt;- cut(zfacet, nbcol)
  persp(x, y, z1, zlim=c(-8, 10.5), col = color[facetcol], phi = 30, theta = -30)
  #zlim added to encompass combined ranges of z1 and z2
  
  #exact same code, with different values of z
  x &lt;- seq(-1.95, 1.95, length = 30)
  y &lt;- seq(-1.95, 1.95, length = 35)
  z2 &lt;- outer(x, y, function(a, b) a*b^2+3)
  nrz &lt;- nrow(z2)
  ncz &lt;- ncol(z2)
  # Create a function interpolating colors in the range of specified colors
  jet.colors &lt;- colorRampPalette( c("blue", "green") )
  # Generate the desired number of colors from this palette
  nbcol &lt;- 100
  color &lt;- jet.colors(nbcol)
  # Compute the z-value at the facet centres
  zfacet &lt;- z2[-1, -1] + z2[-1, -ncz] + z2[-nrz, -1] + z2[-nrz, -ncz]
  # Recode facet z-values into color indices
  facetcol &lt;- cut(zfacet, nbcol)
  persp(x, y, z2, zlim=c(-8, 10.5), col = color[facetcol], phi = 30, theta = -30)
  #zlim added to encompass combined ranges of z1 and z2
  
  #comparing the values, we see that there's different, overlapping scales from z1 and z2
  summary(as.vector(z1))
  summary(as.vector(z2))
  </code></pre>
  
  <p>So when you look at the two plots of z2, you see that they're on different scales, sort of. It would be a lot clearer if I could get that color scale to cover the ranges of z1 and z2<img src="https://i.stack.imgur.com/lNZIA.png" alt="enter image description here"></p>