(This article was first published on mintgene » R, and kindly contributed to R-bloggers)
Often you want to set the fixed colors for particular range of your dataset to be sure that the visual output is correctly represented. This is particularly useful for time series data, where the range or your dataset might drastically change during the course of the simulation.
To do that in R, we need to set the “breaks” parameter in plotting functions such as image or heatmap.2.
# gplots contains the heatmap.2 function
library(gplots)
# create 50x10 matrix of random values from [-1, +1]
random.matrix <- matrix(runif(500, min = -1, max = 1), nrow = 50)
# following code limits the lowest and highest color to 5%, and 95% of your range, respectively
quantile.range <- quantile(random.matrix, probs = seq(0, 1, 0.01))
palette.breaks <- seq(quantile.range["5%"], quantile.range["95%"], 0.1)
# use http://colorbrewer2.org/ to find optimal divergent color palette (or set own)
color.palette <- colorRampPalette(c("#FC8D59", "#FFFFBF", "#91CF60"))(length(palette.breaks) - 1)
heatmap.2(
random.matrix,
dendrogram = "row",
scale = "none",
trace = "none",
key = FALSE,
labRow = NA,
labCol = NA,
col = color.palette,
breaks = palette.breaks
)
Enjoy plotting! mintgene
To leave a comment for the author, please follow the link and comment on his blog: mintgene » R.
R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series,ecdf, trading) and more...

Zero Inflated Models and Generalized Linear Mixed Models with R.
Zuur, Saveliev, Ieno (2012).
