title: R: Contour Plot is Dropping Points
body: <p>I am graphing a data set of many points in the x, y and z dimensions.  For each x and y there can be multiple z's.  What I am attempting to do is divide the xy-plane into a grid of 50x50 and then graph the maximum values of z for each box in a contour plot.  I thought the code below that I found online worked but, upon closer inspection, it looks like it is dropping points or skewing the data.  Is there something wrong with my code?</p>
  
  <pre><code>df&lt;-eff[c("Xs",Ys","Zs")]
  df$x&lt;-cut(df$Xs, breaks=50, labels=FALSE)
  df$y&lt;-cut(df$Ys, breaks=50, labels=FALSE)
  df.max&lt;-expand.grid(x=1:50, y=1:50)
  df.max&lt;-merge(df.max, aggregate(Zs~x+y, df, max), all.x=TRUE)
  z&lt;-matrix(df.max$Zs, nr=50, nc=50)
  x.values &lt;- min(df$Xs)+(0:49)*diff(range(df$Xs))/50
  y.values &lt;- min(df$Ys)+(0:49)*diff(range(df$Ys))/50
  image2D(x=x.values, y=y.values, z, rasterImage = TRUE, contour = list(lwd = 2, col = jet.col(11)),
      main="Contour Plot", xlab="Xs",ylab="Ys", clab="Zs", ylim=c(0,max(df$Ys)*1.05), 
      xlim=c(0,max(df$Xs)*1.05))
  </code></pre>
  
  <p>According to the plot of just the xy-plane (which I believe is correct), the profile of the contour plot should look like this:</p>
  
  <p><a href="https://i.stack.imgur.com/2nRQ3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2nRQ3.png" alt="enter image description here"></a></p>
  
  <p>Notice that it intersects the y-axis around 90k and there is a minimum around (25k,73k).</p>
  
  <p>My contour plot from the same data looks like this:
  <a href="https://i.stack.imgur.com/0OOoA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0OOoA.png" alt="enter image description here"></a></p>
  
  <p>I do not have sample data to provide at the moment because my model is in the middle of running and I don't want to start it all over.  I can add a sample later if necessary.</p>
  
  <p>here is a link to the <a href="https://drive.google.com/open?id=0B0F1yWDNKi2vcnRTd3FkbndMVDg" rel="nofollow noreferrer">data</a></p>