0

I am trying to create a heat map where the colors of the cells are not relative to the other values in the grid, but are linked to absolute values (a bit like the cell formatting in excel, e.g., green for values between 0.20 and 0.25). I have been playing around with the R package superheat but it is still unclear to me how to provide such a mapping. Any recommendations on how this can be done? Preferably using superheat but hints on how to do it via other packages are fine as well.

UPDATE: For instance, using the mtcars dataset, we get

superheat(mtcars,
          X.text = as.matrix(mtcars),
          legend.breaks = c(min(mtcars), 0, max(mtcars)))

Adding a new row to the dataframe, we get different colors for the exact same values in the original rows:

mtcars.new <- rbind(mtcars, 
                    extrarow = c(-100, 6, 20, 10, 6, 2, 10, 1, 1, 6, 6))

superheat(mtcars.new,
          X.text = as.matrix(mtcars.new),
          legend.breaks = c(min(mtcars.new), 0, max(mtcars.new)))

What I would like to have, is that the colors do not depend on the values present in the dataframe, such that when maps are created for different datasets, they can be directly compared to each other.

user321797
  • 127
  • 4
  • 2
    It's easier to help you if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick May 04 '22 at 14:01
  • I have added a simple example. – user321797 May 04 '22 at 14:55

1 Answers1

0

Here is an example on how you could do if you create a "grpColor" variable depending on your variable value. You could then match colors from ggplot with this variable value. (do not hesite to change factors levels if needed)

### 1- Initiating data-frame
set.seed(1)
dfHeatmap <- data.frame(group1=rep(1:4, times=4), group2=rep(1:4, each=4), value=sample(1:100, 16))

### 2- Selecting colors depending on variable values
dfHeatmap$grpColor <- ifelse(dfHeatmap$value<30, "low", 
                 ifelse(dfHeatmap$value<60, "medium", "high"))

### 3- Heatmap with fill=grpColor option in geom_tile
ggplot(data=dfHeatmap, aes(x=as.factor(group1), y=group2)) + 
  geom_tile(aes(fill=grpColor)) + 
  scale_fill_manual("legend", values = c("#25714f", "#1ea467", "#00e57d")) + 
  xlab("group1") + 
  ylab("group2") + 
  geom_text(aes(label=value)) + 
  theme(legend.position="none", 
        line = element_blank())

enter image description here

Yacine Hajji
  • 1,124
  • 1
  • 3
  • 20