Home > database >  How to visualize the cutoff values with another color on pheatmap()?
How to visualize the cutoff values with another color on pheatmap()?

Time:10-06

In the following dataframe:

df1 <- data.frame(id=c(1, 2, 3, 4, 5, 6, 7),
                  revenue=c(34, 10, 0.40, 49, 43, 55, 99))
df2 <- data.frame(id=c(1, 2, 3, 4, 5, 6, 7),
                  expenses=c(22, 0.26, 31, 40, 20, 25, 22))
df3 <- data.frame(id=c(1, 2, 3, 4, 5, 6, 7),
                  profit=c(12, 10, 0.14, 0.9, 0.8, 15, 16))
df_list <- list(df1, df2, df3)    
test <- Reduce(function(x, y) merge(x, y, all=TRUE), df_list)  
rownames(test) <- test[,1]
test[,1] <- NULL
test

I want all values below 1 to be colored in green (the farther it is from 1 more greenish it is). Values above 1 should be colored in red (vice versa, the greater the value, the more saturated is red)

When I use pheatmap(test, cutree_rows = 1) I just have a gradient from the lowest value up to the highest - blue to red.

CodePudding user response:

To use a 'red->green' colour palette you can use the colorRampPalette() function as shown below, although be aware that it is not color-blind-safe (i.e. a significant proportion of people struggle to differentiate red and green) and other palettes are usually preferred for this reason. also, you will need to scale your data, either by "column" or by "row", to get the 'correct' effect:

library(tidyverse)
library(pheatmap)

test <- data.frame(revenue=c(34, 10, 0.40, 49, 43, 55, 99),
                   expenses=c(22, 0.26, 31, 40, 20, 25, 22),
                   profit=c(12, 10, 0.14, 0.9, 0.8, 15, 16))

# not colour-blind safe
col_palette <- colorRampPalette(colors = c("green", "white", "red"))(30)
pheatmap(test, cutree_rows = 1, color = col_palette,
         scale = "column")


# colour-blind safe
better_col_palette <- viridis::plasma(30)
pheatmap(test, cutree_rows = 1, color = better_col_palette,
         scale = "column")

Created on 2022-10-06 by the reprex package (v2.0.1)

  •  Tags:  
  • r
  • Related