Successful Run
It's possible I do not understand the expectations, however, if I follow the example at stat_contour
's documentation, I successfully get:
# Generate data
library(reshape2) # for melt
volcano3d <- melt(volcano)
names(volcano3d) <- c("x", "y", "z")
# Basic plot
v <- ggplot(volcano3d, aes(x, y, z = z))
v + stat_contour()
leading to:
Unsuccessful Run
However, when I try to do what appears to be exactly the same thing with exactly the same data types, I have errors:
my_plt <- ggplot(diamonds, aes(depth, carat, z=price))
my_plt + stat_contour()
yields:
Warning message:
Computation failed in `stat_contour()`:
(list) object cannot be coerced to type 'double'
Debugging Attempt
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:
volcano3d
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
And...
volcano3d %>% sapply(class) x y z "integer" "integer" "numeric"
diamonds
diamonds %>% select(depth, carat, price) %>% 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
And...
diamonds %>% select(depth, carat, price) %>% sapply(class) depth carat price "numeric" "numeric" "numeric"
I cannot see any reason why I should be getting these errors. Any ideas??
Edit and Update
As per Gregor's comments below, it seems that he is correct:
- The
stat_contour
function can only handle regular data intervals in both the x and y dimensions.
For instance, if we look at volcano3d
, the x & y dimensions of the plot are fully "regular":
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
- in each axis, it follows a full sequence from 1,2,3,...
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.
Oh well! I can always use geom_raster
to do something nearly identical, just with a different visualization.
Thanks!