-1

I am trying to generate a heatmap as the following figure. I have already tried pheatmap and the code is as follows:

breaks_2 <- seq(min(0), max(2), by = 0.1) 
pheatmap::pheatmap(
  mat = data, 
  cluster_cols = F, 
  cluster_rows = F, 
  scale = "column",
  border_color = "white", 
  color = inferno(20), 
  show_colnames = TRUE, 
  show_rownames = FALSE, 
  breaks = breaks_2
)

But this does not seem to work. So far I am understanding I am mistaking with defining break or have to use another package than pheatmap. Any suggestion will be really helpful.

enter image description here

Limey
  • 10,234
  • 2
  • 12
  • 32
Raha
  • 1
  • 1
  • "this does not seem to work" isn't very helpful. What is your desired output? In what way does your graph fail to meet your objectives? – Limey Apr 04 '22 at 09:31
  • @Limey well, I get output but I need a output like the file I attached. If you observe it, all values above 2 have daffodil color in inferno scale. So I set the breaks as seq(0,2) but my output is not like this. Now I am confused should I use another package other than pheatmap? – Raha Apr 04 '22 at 09:50
  • So is the heatmap you linked to produced by the code you provide or in another way? We can't check because you haven't provided your input data. Please lep us to help you by providing a minimal reproducible example. [This post](https://stackoverflow.com/help/minimal-reproducible-example), and many others, will help you. – Limey Apr 04 '22 at 09:57

1 Answers1

0

The color scale in pheatmap adjusts to the range of the input data. If you want anything above a certain value to be coloured daffodil, then simply send pheatmap a copy of your data with the highest values rounded to 2.

Suppose you have a data frame like this, with values anywhere between 0 and 3:

set.seed(1)
data <- as.data.frame(matrix(runif(64, 0, 3), nrow = 8))
names(data) <- LETTERS[1:8]

data
#>           A         B         C        D         E          F         G         H
#> 1 0.7965260 1.8873421 2.1528555 0.801662 1.4806239 2.46283888 2.1969412 0.9488151
#> 2 1.1163717 0.1853588 2.9757183 1.158342 0.5586528 1.94118058 2.0781947 1.5559028
#> 3 1.7185601 0.6179237 1.1401055 0.040171 2.4821200 2.34879829 1.4328589 1.9860152
#> 4 2.7246234 0.5296703 2.3323357 1.147164 2.0054002 1.65910893 2.5836284 1.2204906
#> 5 0.6050458 2.0610685 2.8041157 2.609073 2.3827196 1.58915874 1.3142913 2.7386278
#> 6 2.6951691 1.1523112 0.6364276 1.021047 0.3238309 2.36806870 0.7343918 0.8808101
#> 7 2.8340258 2.3095243 1.9550213 1.446240 2.1711328 0.06999361 0.2120371 1.3771972
#> 8 1.9823934 1.4930977 0.3766653 1.798697 1.2338233 1.43169020 0.2983985 0.9971840

Some of the values are greater than two. We want all of these to appear the same colour on our heatmap, so we create a copy of our data for plotting, and round down all of the values that were greater than 2 to be exactly 2:

data_2   <- data
data_2[] <- lapply(data_2, function(x) { x[x > 2] <- 2; x })

So now if we run pheatmap on data_2, we see that all the values that were greater than 2 in our original data frame are coloured daffodil.

library(viridis)
library(pheatmap)

breaks_2 <- seq(0, 2, by = 0.1) 

pheatmap(
  mat = data_2, 
  cluster_cols = F, 
  cluster_rows = F,
  border_color = "white", 
  scale = 'none',
  color = inferno(22), 
  show_colnames = TRUE, 
  show_rownames = FALSE, 
  legend_breaks = breaks_2
)

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • Hi, @Allan Cameron , thank you for your suggestion. I tried it, but I still need a few suggestions. I would have attached the screenshot here. But they are not letting me adding images in this comment anyways. Can I send you an email or inbox you somewhere to discuss a bit more? Thanks again. – Raha Apr 04 '22 at 12:52