Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding points to a geom_tile layer in ggplot2

Tags:

r

ggplot2

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?

like image 410
essicolo Avatar asked Mar 28 '12 14:03

essicolo


1 Answers

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))
like image 66
smu Avatar answered Sep 27 '22 01:09

smu