I'm having a filled ggplot contour plot, depicting continuous R-squared values on a 100x100 grid. By default the legend depicts the values in a gradient like continuous manner, resulting from the data's continuous nature.
I, however, would like to classify and visualize the data in classes, each covering a range of 0.05 R-squared values (i.e. class 1 = 0.00-0.05, class 2 = 0.05-0.10, etc.). I have tried several commands such as scale_fill_brewer and scale_fill_gradient2. The latter does in fact generate some sort of discrete classes, but the class labels depict the break values rather than the class range. Scale_fill_brewer returns the error that the continuous data is forced on to a discrete scale, which makes sense, although I can't see how to work around it.
To make matters more complex, I prefer to make use of a diversified color palette to allow identification of specific classes more easily. Besides, I have a multitude of different contour plots with different maximum R-squared values. So ideally the code is generic and can be easily used for the other plots as well.
Thus far, this is the code I have:
library(scales)
library(ggplot2)
p1 <- ggplot(res, aes(x=Var1, y=Var2, fill=R2)) +
geom_tile
p1 +
theme(axis.text.x=element_text(angle=+90)) +
geom_vline(xintercept=c(seq(from = 1, to = 101, by = 5)),color="#8C8C8C") +
geom_hline(yintercept=c(seq(from = 1, to = 101, by = 5)),color="#8C8C8C") +
labs(list(title = "Contour plot of R^2 values for all possible correlations between Simple Ratio indices & Nitrogen Content", x = "Wavelength 1 (nm)", y = "Wavelength 2 (nm)")) +
scale_x_discrete(breaks = c("b450","b475","b500","b525","b550","b575","b600","b625","b650","b675","b700","b725","b750","b775","b800","b825","b850","b875","b900","b925","b950")) +
scale_y_discrete(breaks = c("b450","b475","b500","b525","b550","b575","b600","b625","b650","b675","b700","b725","b750","b775","b800","b825","b850","b875","b900","b925","b950")) +
scale_fill_continuous(breaks = c(seq(from = 0, to = 0.7, by = 0.05)), low = "black", high = "green")
The output at this point looks like this: