0

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:

Result_ContourPlot

1
  • Can the cut function also be applied in a generic manner, i.e. allowing it to be called for every contour plot that I'm generating and it automatically adjusting to the range of R-squared values for each particular plot? Commented Dec 18, 2015 at 12:37

1 Answer 1

1

You might what to use scale_fill_gradientn() Untested since you failed to provide a reproducible example

library(scales)
library(ggplot2)
ggplot(res, aes(x=Var1, y=Var2, fill=R2)) +
  geom_tile() + 
  scale_fill_gradientn(
      colours = terrain.colors(15), 
      breaks = seq(from = 0, to = 0.7, by = 0.05)
  )
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Thierry! The code works like a charm. Even though the data remains continuous rather than discrete, the rainbow color palette in particular does notably enhance identification of 'hotspot' areas after all. There's one more task that I have not been able to complete, namely to derive the value and index (x,y) of the tile with the highest (R-squared) value. Additionally I would like to retrieve a list with the value and index of 5% of all the tiles with the highest value, but the first step is already proving complicated enough for now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.