0

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:

proper stat_contour plot

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!

Mike Williamson
  • 4,915
  • 14
  • 67
  • 104
  • 1
    If you look at the class of `volcano` and the class of `diamonds`, I believe diamonds has been changed to a `tbl_df` in recent versions of ggplot. Try using `as.data.frame(diamonds)`. – Gregor Thomas Mar 09 '16 at 00:27
  • Hi @Gregor thanks for the advice! I made the change and tried again. That does not fix it. (Glad to know it didn't fix it! It'd be crazy if a set of data provided in ggplot2 was not, in fact, usable in ggplot2's functions!) – Mike Williamson Mar 09 '16 at 01:22
  • Hmmm, the obvious difference between `volcano3d` and `diamonds` is the regular spacing of points in volcano. I don't have an *answer* yet, but you get the same Warning and empty plot if you duplicate a row, e.g., using this data: `v3d = rbind(volcano3d, volcano3d[1, ])`. Diamonds doesn't have any duplicate rows, but maybe up to rounding... – Gregor Thomas Mar 09 '16 at 02:01
  • Thanks, @Gregor ! I was thinking that might indeed be the case. But if so, that clearly seems to tend towards bug, not feature. That doesn't help me in the immediate term, but it does make me think it's worth submitting a github issue for ggplot2. – Mike Williamson Mar 09 '16 at 06:25
  • Hi @MikeWilliamson, I'm trying to plot 3d data (w/o duplicate coordinates) which is not on a regular grid but failing at a workaround with geom_tile (that you mentioned worked for you). Could you possibly post your workaround to get a contour map for the diamonds data set? – mabe Mar 06 '20 at 14:14
  • @mabe, I am sorry, I cannot remember and do not have the code any more. I do not use R much these days any more. From what I remember, though, stat_contour is just not as robust as most of the other ggplot2 plot types. So maybe try to convert it to a heatmap? – Mike Williamson May 05 '20 at 10:20
  • @MikeWilliamson, thanks anyway. I think I went for kriging in the end to interpolate the data to a regular grid before plotting. – mabe May 08 '20 at 07:46

0 Answers0