2

I have a matrix

library(pheatmap)
set.seed(1)
mat <- matrix(rexp(200, rate=.001), ncol=20)
pheatmap(mat)

and there is one value that is much higher than the rest. Therefore, I would like to use a logarithmic scaling for the legend bar (1, 10, 100, 1000, ...).

Is there a possibility to do that with the pheatmap package?

EDIT: I don't want to make log(mat), I only want the color scaling bar be scaled logarithmic (1, 10, 100, 1000, ...).

Revan
  • 2,072
  • 4
  • 26
  • 42

1 Answers1

4

Just add logarithmic legend_breaks and show a label for the maximum.

pheatmap::pheatmap(mat, legend_breaks=c(10^(0:ceiling(log10(max(mat)))), 
                                        round(max(mat), 2)))

Yielding

plot

jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • This doesn't space the breakpoints on the legend in a logarithmic fashion. Either this isn't a solution or I didn't understand the question. – flies Nov 28 '22 at 16:14
  • @flies It only considers values in the data and also shows the maximum which is below what the next logarithmic breakpoint, 10K, would be. That's probably what is confusing you. – jay.sf Nov 28 '22 at 18:11
  • if you instead use `rexp(400...)` you get `legend_breaks==c(1,10,100, 1000, 10k, 6331.28)`, and you'll see that the 10 sits right on top of the 100 instead of having equal distance between 10, 100, and 1000. – flies Dec 01 '22 at 14:42