I am trying to use the solution that @jlhoward provided here to make a contour plot in ggplot with discretely defined contour intervals. However, my data set crosses zero and this seems to cause the colors and labels of the values below zero to be plotted out of order.
x<-seq(-11,11,.03) # note finer grid
y<-seq(-11,11,.03)
xyz.func<-function(x,y) {-10.4+6.53*x+6.53*y-0.167*x^2-0.167*y^2+0.0500*x*y}
gg <- expand.grid(x=x,y=y)
gg$z <- with(gg,xyz.func(x,y)) # need long format for ggplot
library(ggplot2)
library(RColorBrewer) #for brewer.pal()
brks <- cut(gg$z,breaks=seq(-50,100,len=6))
brks <- gsub(","," - ",brks,fixed=TRUE)
gg$brks <- gsub("\\(|\\]","",brks) # reformat guide labels
ggplot(gg,aes(x,y)) +
geom_raster(aes(fill=brks))+
scale_fill_manual("Z",values=brewer.pal(6,"YlOrRd"))+
scale_x_continuous(expand=c(0,0))+
scale_y_continuous(expand=c(0,0))+
coord_fixed()
This produces this plot:
As you can see, the colors and the labels for the top two contours are backwards. Any suggestions on how to fix this?
PS I hope the link to the image works. It looks like I need more reputation points before I can include images in a post :(