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

*Related*

To

**leave a comment** for the author, please follow the link and comment on their blog:

** mintgene » R**.

R-bloggers.com offers

**daily e-mail updates** about

R news and

tutorials on topics such as:

Data science,

Big Data, R jobs, visualization (

ggplot2,

Boxplots,

maps,

animation), programming (

RStudio,

Sweave,

LaTeX,

SQL,

Eclipse,

git,

hadoop,

Web Scraping) statistics (

regression,

PCA,

time series,

trading) and more...

If you got this far, why not

__subscribe for updates__ from the site? Choose your flavor:

e-mail,

twitter,

RSS, or

facebook...