I have a dataframe with columns X, Y, VALUE. I use ggplot2 to plot the values in the specified coordinates. But I want to fill in the color based on custom ranges for the VALUE column. For instance:
VALUE COLOUR
0 to 2 blue
2 to 5 green
5 to 10 red
10 to 20 yellow
20 to 30 orange
30 to 40 grey
>40 black
How to fill a specific color for specific values using ggplot2?
I can change your data, if necessary, and match the individual re-selected value with the colors after that, for example:
VALUE RESAMPLED VALUE COLOUR
0 to 2 10 blue
2 to 5 20 green
5 to 10 30 red
10 to 20 40 yellow
20 to 30 50 orange
30 to 40 60 grey
>40 70 black
But the values are not displayed in colors with this code:
ggplot2::ggplot() + ggmap::theme_nothing() +
ggplot2::geom_tile(data = xyDataFrame, alpha = 0.6, aes(x = X, y = Y, fill=VALUE)) +
ggplot2::scale_fill_gradientn(colours = c("blue", "green", "red", "yellow", "orange", "grey", "black"))
source
share