ggplot2's stat_contour and geom_contour throwing errors attempting to use z axis
<h1>Successful Run</h1>

<p>It's possible I do not understand the expectations, however, if I follow the example at <a href="http://docs.ggplot2.org/0.9.2.1/stat_contour.html" rel="nofollow noreferrer"><code>stat_contour</code>'s documentation</a>, I successfully get:</p>

<pre><code># Generate data
library(reshape2) # for melt
volcano3d &lt;- melt(volcano)
names(volcano3d) &lt;- c("x", "y", "z")

# Basic plot
v &lt;- ggplot(volcano3d, aes(x, y, z = z))
v + stat_contour()
</code></pre>

<p>leading to:</p>

<p><a href="https://i.stack.imgur.com/GpHiR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GpHiR.png" alt="proper stat_contour plot"></a></p>

<h1>Unsuccessful Run</h1>

<p>However, when I try to do what appears to be <strong><em>exactly the same thing with exactly the same data types</em></strong>, I have errors:</p>

<pre><code>my_plt &lt;- ggplot(diamonds, aes(depth, carat, z=price))
my_plt + stat_contour()
</code></pre>

<p>yields:</p>

<pre><code>Warning message:
Computation failed in `stat_contour()`:
(list) object cannot be coerced to type 'double' 
</code></pre>

<h1>Debugging Attempt</h1>

<p>The only potential difference I could imagine, especially with the error I was receiving, was an object type difference.  So, here are some details there:</p>

<ul>
<li><p><code>volcano3d</code></p>

<pre><code>head(volcano3d)

  x y   z
1 1 1 100
2 2 1 101
3 3 1 102
4 4 1 103
5 5 1 104
6 6 1 105
</code></pre>

<p>And...</p>

<pre><code>volcano3d %&gt;% sapply(class)
     x         y         z 
"integer" "integer" "numeric" 
</code></pre></li>
<li><p><code>diamonds</code></p>

<pre><code>diamonds %&gt;% select(depth, carat, price) %&gt;% head
Source: local data frame [6 x 3]

  depth carat price
  (dbl) (dbl) (dbl)
1  61.5  0.23   326
2  59.8  0.21   326
3  56.9  0.23   327
4  62.4  0.29   334
5  63.3  0.31   335
6  62.8  0.24   336
</code></pre>

<p>And...</p>

<pre><code>diamonds %&gt;% select(depth, carat, price) %&gt;% sapply(class)
    depth     carat     price 
"numeric" "numeric" "numeric" 
</code></pre></li>
</ul>

<p>I cannot see any reason why I should be getting these errors.  Any ideas??</p>

<h1>Edit and Update</h1>

<p>As per Gregor's comments below, it seems that he is correct:</p>

<ul>
<li>The <code>stat_contour</code> function can only handle regular data intervals in both the x and y dimensions.</li>
</ul>

<p>For instance, if we look at <code>volcano3d</code>, the x &amp; y dimensions of the plot are fully "regular":</p>

<pre><code>volcano3d[seq(1,101,10),]

     x y   z
1    1 1 100
11  11 1 109
21  21 1 122
31  31 1 114
41  41 1 107
51  51 1 115
61  61 1 113
71  71 1 114
81  81 1  99
91   4 2 103
101 14 2 113
</code></pre>

<ul>
<li>in each axis, it follows a full sequence from 1,2,3,...</li>
</ul>

<p>It's very rare that I have data in such a regular format.  This makes the function fairly limited in application.  Although I suppose data can be aggregated, binned and then thrown back into numerical formats in both the axes, to allow for some jerry-rigged attempt.</p>

<p>Oh well!  I can always use <code>geom_raster</code> to do something nearly identical, just with a different visualization.</p>

<p>Thanks!</p>