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()
The problem of interpolating colors with a complex color scale is best illustrated on the "old faithful" data.
If you use your user defined "rainbow" color scale, the interpolation will not be doing so great (even if the result still looks quite good).
Without interpolation
library(ggplot2)
cols <- rev(rainbow(7)[-7])
ggplot(faithfuld, aes(waiting, eruptions)) +
geom_raster(aes(fill = density), interpolate = FALSE) +
scale_fill_gradientn(colours = cols)
With interpolation
ggplot(faithfuld, aes(waiting, eruptions)) +
geom_raster(aes(fill = density), interpolate = TRUE) +
scale_fill_gradientn(colours = cols)
With interpolation and a less complex palette
ggplot(faithfuld, aes(waiting, eruptions)) +
geom_raster(aes(fill = density), interpolate = TRUE) +
scale_fill_gradientn(colours = c("steelblue", "tomato"))
ggheatmap <- ggplot(data = dat.plot, aes(x=Var1, y=Var2, fill=value)) +
geom_raster(interpolate=TRUE)+
scale_fill_gradientn(colors=rev(c("darkred", "red", "orange", "yellow", "green", "lightgreen", "lightblue", "darkblue")))+
theme(axis.text.x = element_text(angle = 0))+
coord_fixed()
print(ggheatmap)
Now it looks blurred. But o.k. If you think it can't be done better, I let it as it is. Thank you very much!