I can hardly think of a different way than to map your desired colors to specific value ranges. See below. Please check how I reduced your code, there were lots of unnecessary calls, (I guess you've copied it from a script where you have tried different stuff). Also, I have changed the colorRampPalette call - this is a function generator, no need to use function()
here.
Notice you would need to manually define the values, and I guess this would be your researcher decision how to present the data. You need to scale it to a range 0:1
library(RColorBrewer)
library(dplyr)
library(ggplot2)
myData <- matrix(c(2,2,3,3,3,1,2,2,3,3,1,1,2,2,3,1,1,2,2,2,1,1,1,1,2), nrow = 5, ncol = 5, byrow = TRUE)
longData <- reshape2::melt(myData)
colnames(longData) <- c("Likelihood", "Consequence", "value")
longData <- mutate(longData, value = Consequence * Likelihood)
mycols <- rev(c("red4","red2","tomato2","orange","gold1","forestgreen"))
cols <- colorRampPalette(mycols)
myvals <- c(0, 8, 9, 10, 11, 25)
scaled_val <- scales::rescale(myvals, 0:1)
ggplot(longData, aes(x = Consequence, y = Likelihood, fill = value)) +
geom_tile() +
scale_fill_gradientn(colours = cols(length(mycols)),
values = scaled_val) +
theme(axis.text.y = element_text(angle = 90, hjust = 1), legend.position = "none") +
scale_x_continuous(name = "Probability", breaks = seq(1, 5, 1), expand = c(0, 0)) +
scale_y_reverse(name = "Severity", breaks = seq(1, 5, 1), expand = c(0, 0)) +
geom_hline(yintercept = seq(1.5, 5.5)) +
geom_vline(xintercept = seq(1.5, 5.5)) +
coord_fixed()
Additionally you can define from where your gradient shall start. I've shown how to do this I a very recent thread. Note your desired output does not match the values (I've superimposed them to demonstrate that). Also notice this all is obviously all defined by yourself - those values that I chose are random, and it's up to you to tweak it to your liking.
myvals <- c(0, 6, 7, 9, 10, 11, 25)
scaled_val <- scales::rescale(myvals, 0:1)
ggplot(longData, aes(x = Consequence, y = Likelihood, fill = value)) +
geom_tile() +
geom_text(aes(label = value)) +
scale_fill_gradientn(colours = c(mycols[1], mycols),
values = scaled_val) +
theme(axis.text.y = element_text(angle = 90, hjust = 1), legend.position = "none") +
scale_x_continuous(name = "Probability", breaks = seq(1, 5, 1), expand = c(0, 0)) +
scale_y_reverse(name = "Severity", breaks = seq(1, 5, 1), expand = c(0, 0)) +
geom_hline(yintercept = seq(1.5, 5.5)) +
geom_vline(xintercept = seq(1.5, 5.5)) +
coord_fixed()