First of all I have to say that I read many threads about heatmap and ggplot2 here in stackoverflow and elsewhere. But my problem isn\'t solved yet.
I have got the foll
Short answer: function rainbow()
goes nuts when you pass 100
as you're asking for 100
different colors.
What should you do: pass n
to rainbow()
for how many colors you want. If you want to go from blue to red then you also have to wrap it with function rev()
.
library(egg)
library(ggplot2)
library(reshape2)
# Heatmap number of rows/columns
Nvalue <- 1e2
# n for colors passed to function rainbow
nColor <- c(1:10, seq(20, 100, 20))
# dummy data
df <- melt(matrix(rnorm(N^2), N))
plotList <- list()
for(i in seq_along(nColor)) {
plotList[[i]] <- ggplot(df, aes(Var1, Var2, fill = value)) +
geom_raster() +
scale_fill_gradientn(colours = rev(rainbow(nColor[i]))) +
labs(title = paste0("rainbow(", nColor[i], ")"),
x = NULL,
y = NULL,
fill = NULL) +
theme_void()
}
ggarrange(plots = plotList)
Edit:
After OP specified colors he wants then passing hex vector should work:
hex <- c("#FF0000", "#FFA500", "#FFFF00", "#008000", "#9999ff", "#000066")
ggplot(df, aes(Var1, Var2, fill = value)) +
geom_raster() +
scale_fill_gradientn(colours = rev(hex)) +
labs(x = NULL,
y = NULL,
fill = NULL) +
theme_void()