My question is I want a divergent color for my heatmap using geom_tile
and the gradient color varies on both ends of the scale. For example, the whole scale is (-1,1), I only want the values from -1 to -0.5 and the values from 0.5 to 1.0 have gradient color variation and the values between -0.5 and 0.5 stays as white. However I can't find an option in scale_fill_gradient
to reach the goal. A reproducible example is as below and the data is from ggplot2 heatmaps: using different gradients for categories
nba <- read.csv("http://datasets.flowingdata.com/ppg2008.csv")
nba$Name <- with(nba, reorder(Name, PTS))
library("ggplot2")
library("plyr")
library("reshape2")
library("scales")
nba.m <- melt(nba)
nba.s <- ddply(nba.m, .(variable), transform,
rescale = scale(value))
ggplot(nba.s, aes(variable, Name))+geom_tile(aes(fill = rescale), colour = "white") +
scale_fill_gradient(low = "darkgreen", high = "darkred")
Best Answer
You can try adding a white midpoint to
scale_fill_gradient2
:But, you'll have the most flexibility if you follow the answer in the SO post you linked to and use
scale_fill_gradientn
.EDIT (to show an example from the comment discussion)
The legend is far from ideal, but you can potentially exclude it or work around it through other means.