I've found the following R code in the R graph gallery (https://www.r-graph-gallery.com/79-levelplot-with-ggplot2.html) for a heatmap and modified it a little bit:
# Library
library(ggplot2)
set.seed(10)
# Dummy data
x <- LETTERS[1:20]
y <- paste0("var", seq(1,20))
data <- expand.grid(X=x, Y=y)
data$Z <- runif(400, -1, 2)
print (data)
# Heatmap
ggplot(data, aes(X, Y, fill= Z)) +
geom_tile(color = "white",
lwd = 0.5,
linetype = 1)
My issue: I have three columns with values ranging from -1 to 2. Now I would like to assign defined colors to the values, f.e. as follows: -1: color red, 0: color green, 1: color yellow, 2: color blue.
Is there a way to use the geom_tile function for my issue?
Thank you!