在ggplot2中为geom_tile层添加点

ess*_*olo 4 r ggplot2

在R 2.13.1/ggplot2 0.8.9中,我试图将点添加到geom_tile层.此示例再现错误.

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
Run Code Online (Sandbox Code Playgroud)

什么是错的?

小智 11

要么取消z美学的映射

v + geom_tile(aes(fill = z)) + geom_point(data=pts, aes(x=a, y=b,z=NULL) )
Run Code Online (Sandbox Code Playgroud)

或者只是从第一个ggplot调用中删除它

v <- ggplot(volcano3d, aes(x, y))
v + geom_tile(aes(fill = z)) + geom_point(data=pts, aes(x=a, y=b))
Run Code Online (Sandbox Code Playgroud)