Laurie
Well done. I have replicated your code, and the two graphs are
virtually identical, except of course the ggplot graph looks
better ;-)
A small observation regarding writing robust code: There is an
interp() function in both the libraries akima and ggplot2. This means
that the code behaves different depending on the order in which the
libraries are loaded. In my case, I loaded ggplot2 after akima, and
the code threw an error. To get round this, I had to modify the call
to interp() to akima::interp()
library(akima)
library(ggplot2)
x<-melt(volcano)
x$value<-x$value-min(x$value)
x$value<-x$value/max(x$value)
x<-x[sample(1:dim(x)[1],100),]
##### plot
t. <- akima::interp(x[,"X1"], x[,"X2"], x[,"value"],
xo=seq(min(x[,"X1"]), max(x[,"X1"]), length=101),
yo=seq(min(x[,"X2"]), max(x[,"X2"]),length=101))
image( t.,breaks=c(0,.5,.70,1), col=c("red","yellow","green"))
contour(t.,levels=c(.5,.6,.70,.9), add=T,
col=c("black","grey","black","grey"), lwd=2, method="edge",
labcex=1)
gt <- data.frame(
expand.grid(X1=t.$x,
X2=t.$y),
z=c(t.$z),
value=cut(c(t.$z),
breaks=c(0,.5,.75,1)))
ggplot(gt) +
geom_tile(aes(X1,X2,fill=value)) +
geom_contour(aes(x=X1,y=X2,z=z))+
scale_fill_manual(values=c("red","yellow","green"),name="Probability")
###
Regards
Andrie