I'm attempting to generate a heatmap with divergent colors (red, white, blue) but with the white at a selected value (or shifting the scale), instead of the automatic fitting/centering of the data.
Sample dataset:
set.seed(5)
demo <- data.frame(x = seq(from = -40, to = 40, by = 5), y = 0:5, data = runif(102, min = 0, max = 1))
My attempts at generating a heatmap are based on:
library(ggplot2)
library(ggsci)
ggplot(demo) + geom_tile(aes(x = x, y = y, fill = data), color = NA) + scale_fill_gsea()
However, I'd like to shift the center "white" part at a different value, for example 0.9 and I'd like to set my own discrete bins.
From reading further articles and stackoverflow posts, my understanding is that I'm going to have to use cut
to generate my own sections before using scale_fill_manual
in order to set my own color scales.
Is there an easier method to this that I'm missing?
Thanks.
With scale_fill_gradient2
you can define a custom value for the midpoint:
ggplot(demo) +
geom_tile(aes(x = x, y = y, fill = data), color = NA) +
scale_fill_gradient2(low = "blue", mid = "white", high = "red",
midpoint = 0.9,
breaks = seq(0, 1, 0.1),
limits = c(0, 1))