Q:

Conditional Colouring of Matrix Cells in R

I have a matrix in R, for example:
mat <- matrix(round(runif(n=30^2, min=0, max=1),2), nrow=30, ncol=30)
My aim is to colour the cells of this matrix, dependent on the value in the cell. The values are numeric, not factors. For example: I like to colour all values between 0.4 and 0.6 red, if possible such that the values are still shown in the cell. I like to save the matrix as an image - the aim is to see at first glance which cells are red. The solution of this problem should not only allow one colour: It may be useful later to say that values between 0 and 0.2 are green, or additionally values between 0.8 and 1 black etc.
I tried the following things:
Conditional coloring of cells in table
-> Gives me a heatmap, but I like to choose which values get which colour
How to color specific cells in a Data Frame / Table in R?
-> Gives me a HTML table, but not an Image (I cannot save it in a way that I can see the whole table)
http://www.phaget4.org/R/image_matrix.html
-> Same problem as in first link
Do you know how I can get that?

A:

Here's an example:
library(ggplot2)
library(reshape2)
ggplot(melt(mat), aes(Var1, Var2, fill=cut(value, seq(0, 1, .2)), label=round(value, 1))) + 
  geom_tile() + 
  scale_fill_manual(values=c("green", "white", "red", "white", "black")) + 
  geom_text(color="orange")

