Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stat_contour and geom_contour throwing errors when attempting to use z axis #1577

lazarillo opened this issue Mar 9, 2016 · 2 comments
bug an unexpected problem or unintended behavior


Copy link

I also submitted this via StackOverflow, since I wasn't 100% sure if the problem were user error or an actual problem with the code.

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()


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

      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


    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


    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??


@hadley hadley added this to the v2.2.0 milestone Jul 28, 2016
@hadley hadley added bug an unexpected problem or unintended behavior ready labels Jul 28, 2016
@hadley hadley closed this as completed in 271e31a Jul 28, 2016
@hadley hadley removed the ready label Jul 28, 2016
Copy link

hadley commented Jul 28, 2016

The problem is that you need to provide a surface - i.e. for each value of x and y you should only have one z value. I've tweaked the docs and added an message to detect this case, but I suspect you're looking for a different geom (or need to do some preprocessing first).

Copy link

Understood, thanks!

It wasn't really much of a problem for me, because the geom_raster(), geom_tile(), and geom_rect() functions are so flexible & powerful that I could just use them to accomplish the nearly same thing, and then effectively make a "filled contour", like your last example on the docs:

v + geom_raster(aes(fill = density)) + geom_contour(colour = "white").

(Except without the + geom_contour(colour = "white")... removing the geom_countour() does not impact the quality of the plot too much.)

@lock lock bot locked as resolved and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
bug an unexpected problem or unintended behavior
None yet

No branches or pull requests

2 participants