r - Error with contour plot ggplot2 -


the following code works fine

dat1 <- data.frame(x=c(-1,-1,1,1),y=c(-1,1,-1,1),z=c(1,2,3,4)) dat2 <- data.frame(x=c(-0.5,0.5),y=c(-0.5,0.5))  ggplot(dat1, aes(x=x, y=y, z=z)) + geom_tile(aes(fill=z)) + scale_fill_gradient(limits = c(0, 1), low = "yellow", high = "red") 

however, this

ggplot(dat1, aes(x=x, y=y, z=z)) + geom_tile(aes(fill=z)) + scale_fill_gradient(limits = c(0, 1), low = "yellow", high = "red") + geom_point(data=dat2, aes(x=x,y=y)) 

gives error

error: aesthetics must either length 1 or same data (2): x, y, z

can please explain why? thanks.

the ggplot function pass of aesthetics stated within aes function nested within geom functions follow it. illustrate point both of following work.

cleanest answer remove z aes function needed first geom.

ggplot(dat1, aes(x=x, y=y) )+   geom_tile(aes(fill=z)) +   scale_fill_gradient(limits = c(0, 1), low = "yellow", high = "red") +   geom_point(data=dat2) 

as there no column z in dat2 explicitly state in aes function geom_point

ggplot(dat1, aes(x=x, y=y, z=z) )+   geom_tile(aes(fill=z)) +   scale_fill_gradient(limits = c(0, 1), low = "yellow", high = "red") +   geom_point(data=dat2, aes( z=null)) 

Comments

Popular posts from this blog

linux - Could not find a package configuration file provided by "Qt5Svg" -

simple.odata.client - Simple OData Client Unlink -