1

I've found the following R code in the R graph gallery (https://www.r-graph-gallery.com/79-levelplot-with-ggplot2.html) for a heatmap and modified it a little bit:

# Library
library(ggplot2)

set.seed(10)

# Dummy data
x <- LETTERS[1:20]
y <- paste0("var", seq(1,20))
data <- expand.grid(X=x, Y=y)
data$Z <- runif(400, -1, 2)

print (data)

# Heatmap 
ggplot(data, aes(X, Y, fill= Z)) + 
  geom_tile(color = "white",
            lwd = 0.5,
            linetype = 1)

My issue: I have three columns with values ranging from -1 to 2. Now I would like to assign defined colors to the values, f.e. as follows: -1: color red, 0: color green, 1: color yellow, 2: color blue.

Is there a way to use the geom_tile function for my issue?

Thank you!

MDStat
  • 355
  • 2
  • 17

2 Answers2

1

If you want to have discrete intervals and scale, you should get the values of df$Z into integer factors and then use scale_fill_manual to get the desired colour scheme.

data$Z <- factor(round(data$Z))

# Heatmap 
ggplot(data, aes(X, Y, fill= Z)) + 
    geom_tile(color = "white",
              lwd = 0.5,
              linetype = 1)+
    scale_fill_manual(values = c('red', 'green', 'yellow', 'blue'))

#or simply

ggplot(data, aes(X, Y, fill= factor(round(data$Z)))) + 
    geom_tile(color = "white",
              lwd = 0.5,
              linetype = 1)+
    scale_fill_manual(values = c('red', 'green', 'yellow', 'blue'), name = 'Z')

enter image description here

To transform the Z values into strings you can use:

library(plyr)

data$Z <- factor(round(data$Z))

ata$Z <- revalue(data$Z, c('-1'='negative'))
data$Z <- revalue(data$Z, c('0' = 'no'))
data$Z <- revalue(data$Z, c('1' = 'yes'))
data$Z <- revalue(data$Z, c('2' = 'other'))

# Heatmap 
ggplot(data, aes(X, Y, fill= Z)) + 
    geom_tile(color = "white",
              lwd = 0.5,
              linetype = 1)+
    scale_fill_manual(values = c('red', 'green', 'yellow', 'blue'), name = 'Z')

enter image description here

Pedro Alencar
  • 1,049
  • 7
  • 20
  • Hi! Thank you very much for the update. Unfortunately I get an error "Continuous value supplied to discrete scale". What do I do wrong? Another question: I like the type of your legend (no gradient but clear breaks), but how could you really assign the values to the altered labels - MrFlick did this this way: breaks=c(-1, 0, 1, 2), colors=c("red","green","yellow","blue"). Could you maybe apply this to your code? Thank you! – MDStat Jul 08 '21 at 13:36
  • Hi MDstat! You have to transform `data$Z` into factors to avoid this error. Try to run the code as it is in your console, particularly the second part. This step is necessary for the `scale_fill_manual` to work properly. – Pedro Alencar Jul 08 '21 at 13:38
  • Hi! Thank you - that was obviously my mistake - it works. If you have time could you maybe comment my last question about direct assignment of values to colors? How could I apply the code "breaks=c(-1, 0, 1, 2), colors=c("red","green","yellow","blue")" to your solution? Thank you so much! – MDStat Jul 08 '21 at 14:03
  • If you convert the values of `Z` to factors you don't need to define `breaks`. Breaks are only used for continuous values. Factors don't require breaks and you should use `scale_fill_manual` – Pedro Alencar Jul 08 '21 at 14:07
  • In time, if the answers were helpfull it would be great if you could upvote it and mark as the correct answer :D – Pedro Alencar Jul 08 '21 at 14:16
  • 1
    Thank you for your answer - everything clear. I marked your answer as successful! But I have another problem - could you maybe help me again, if you have time? I post another reply to this topic because with my new code the character limit is exceeded... – MDStat Jul 09 '21 at 08:29
0

you can use scale_gradient_n for that

ggplot(data, aes(X, Y, fill= Z)) + 
  geom_tile(color = "white",
            lwd = 0.5,
            linetype = 1) + 
  scale_fill_gradientn(breaks=c(-1, 0, 1, 2), colors=c("red","green","yellow","blue"))

Those colors seem to produce quite the sight enter image description here

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Thank you - it works. Now I only need to change the labels in the legend - like: -1 = negative, 0 = no, 1 = yes, 2 = other Is there a possibility? – MDStat Jul 08 '21 at 08:20