I quite like the look and feel of ggplot2
and use them often to display raster data (e.g facetting over timesteps for time-varying precipitation fields is very useful).
However, I'm still wondering whether it is easily possible to bin the continuous raster values into discrete bins and assign to each bin a single colour, that is shown in the legend (as many GIS systems do).
I tried with the guide = "legend"
, and breaks
arguments of the scale_fill_gradient
option. However these affect just the legend on the side of the graph, but the plotted values are still continuous.
library(ggplot2)
data <- data.frame(x=rep(seq(1:10),times = 10), y=rep(seq(1:10),each = 10), value = runif(100,-10,10))
ggplot(data = data, aes(x=x,y=y)) +
geom_raster(aes(fill = value)) +
coord_equal() +
scale_fill_gradient2(low = "darkred", mid = "white", high = "midnightblue",
guide = "legend", breaks = c(-8,-4,0,4,8))
My question is mainly how to discretize the data that is plotted in ggplot
, so that the reader of the graph can make quantitative conclusions on the values represented by the colors.
Secondly, how can I still use a diverging color palette (similar to scale_fill_gradient2
), that is centered around zero or another specific value?