1

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?

Community
  • 1
  • 1
  • 3
    Please share some code. We shouldn't have to navigate between questions to understand which code(s) you applied. –  Jan 19 '16 at 09:02
  • Have a look at *scale_colour_manual* in ggplot2. If I understand correctly, heat map with defined colour palette would meet your requirements. Even simpler, you can make use of *ggthemes* and use one of available colour palettes if it meets your requirements. – Konrad Jan 19 '16 at 09:11

2 Answers2

0

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")
lukeA
  • 53,097
  • 5
  • 97
  • 100
  • Thank you very much lukeA! I never would have got that on my own. Do you know by chance how I could label the rows and columns in the image? (my matrix has rownames and column-names) – Milky Way Jan 19 '16 at 13:28
  • Check out the `breaks` and `labels` arguments of `scale_x_*` and `scale_y_*`. For example you could add `+ scale_x_continuous(breaks=seq(ncol(mat)), labels=c(letters, LETTERS)[seq(ncol(mat))])` to the plot above. – lukeA Jan 19 '16 at 14:15
0

Here is an example using heatmap.2 (gplots):

library (gplots)
mat <- matrix(round(runif(n=30^2, min=0, max=1),2), nrow=30, ncol=30)
#Let's use a submatrix to play
minimat <- mat[1:5,1:5]

# Using cut to build a factor with the different groups
# and change it to 1 to 3 scale
groups <- cut(minimat,c(0,0.4,0.6,1))
levels(groups) <- 1:length(levels(groups))
groups<-matrix(as.numeric(groups), nrow=5)

# Build your palette
my_palette <- c("green","red", "blue")

# In heatmap.2 use groups as values to color with your palette 
# and minimat to display values.
heatmap.2(x = groups, Rowv = FALSE, Colv = FALSE, col = my_palette, dendrogram = "none", cellnote = minimat, notecol = "black", notecex = 2, trace = "none", key = FALSE, margins = c(2, 2))
anpefi
  • 83
  • 6