6

In R 2.13.1 / ggplot2 0.8.9, I'm trying to add points onto a geom_tile layer. This example reproduces the error.

volcano3d <- melt(volcano) 
names(volcano3d) <- c("x", "y", "z") 
pts <- data.frame(a=runif(10,0,80), b=runif(10,0,60))
v <- ggplot(volcano3d, aes(x, y, z = z)) 

v + geom_tile(aes(fill = z))
# works fine

v + geom_tile(aes(fill = z)) + geom_point(data=pts, aes(x=a, y=b)) 
# Error in eval(expr, envir, enclos) : object 'z' not found

Any idea on what is wrong?

essicolo
  • 803
  • 7
  • 13

1 Answers1

13

either unmap the z aesthetics with

v + geom_tile(aes(fill = z)) + geom_point(data=pts, aes(x=a, y=b,z=NULL) )

or simply remove it from the first ggplot call

v <- ggplot(volcano3d, aes(x, y))
v + geom_tile(aes(fill = z)) + geom_point(data=pts, aes(x=a, y=b))
smu
  • 8,757
  • 2
  • 19
  • 14
  • The comment about removing the fill from the first ggplot call was exactly what I was looking forward! Thx for that! – Shadow Aug 12 '14 at 10:18