<p>Looking at the <a href="https://www.rdocumentation.org/packages/pheatmap/versions/1.0.12/topics/pheatmap" rel="nofollow noreferrer">pheatmap documentation</a>, it looks like the way to do this is with the <code>breaks</code> argument, which needs to have one more element than the number of defined colours:</p>
<blockquote>
<p><strong>breaks</strong></p>
<p>a sequence of numbers that covers the range of values in mat and is one element longer than color vector. Used for mapping values to colors. Useful, if needed to map certain values to certain colors, to certain values. If value is NA then the breaks are calculated automatically. When breaks do not cover the range of values, then any value larger than max(breaks) will have the largest color and any value lower than min(breaks) will get the lowest color.</p>
</blockquote>
<pre><code>color.divisions &lt;- 100
color &lt;- makeColorRampPalette(c(&quot;navy&quot;, &quot;white&quot;,&quot;white&quot;, &quot;red&quot;), 
                             cutoff.distance / max(distmat), color.divisions)

pheatmap(res$r, 
    main = &quot;Correlation Matrix&quot;,
    annotation_col = Heat_test, 
    fontsize = 18,
    breaks = seq(-1,1, length.out=(color.divisions + 1)), 
    color = color, 
    show_rownames = F, 
    show_colnames = F, 
    legend = T)
</code></pre>

CC BY-SA 4.0
2024-05-16T20:13:11.043Z
2024-05-16T20:13:11.043Z
gringer
<p>I'm using pheatmap to generate a correlation matrix in R. Ideally, I want my colour scale to run from -1.0 - 1.0 but my lowest negative correlation is 0.6. Is there a way to manually set the range?</p>
<pre><code>color &lt;- makeColorRampPalette(c(&quot;navy&quot;, &quot;white&quot;,&quot;white&quot;, &quot;red&quot;), 
                             cutoff.distance / max(distmat),100)

   pheatmap(res$r, 
    main = &quot;Correlation Matrix&quot;,
    annotation_col = Heat_test, 
    fontsize = 18, 
    color = color, 
    show_rownames = F, 
    show_colnames = F, 
    legend = T)
</code></pre>

<r><heatmap>
Manually set range of colour scale in PHeatmap in R
# Manually set range of colour scale in PHeatmap in R

(Asked by: username_0 on 2024-05-16)

I'm using pheatmap to generate a correlation matrix in R. Ideally, I want my colour scale to run from -1.0 - 1.0 but my lowest negative correlation is 0.6. Is there a way to manually set the range?
color <- makeColorRampPalette(c("navy", "white","white", "red"), 
                             cutoff.distance / max(distmat),100)

   pheatmap(res$r, 
    main = "Correlation Matrix",
    annotation_col = Heat_test, 
    fontsize = 18, 
    color = color, 
    show_rownames = F, 
    show_colnames = F, 
    legend = T)

**Tags:** r, heatmap

---

## 1 answers

### Answer by username_1 on 2024-05-16. Score: 2

Looking at the pheatmap documentation, it looks like the way to do this is with the breaks argument, which needs to have one more element than the number of defined colours:

breaks
a sequence of numbers that covers the range of values in mat and is one element longer than color vector. Used for mapping values to colors. Useful, if needed to map certain values to certain colors, to certain values. If value is NA then the breaks are calculated automatically. When breaks do not cover the range of values, then any value larger than max(breaks) will have the largest color and any value lower than min(breaks) will get the lowest color.

color.divisions <- 100
color <- makeColorRampPalette(c("navy", "white","white", "red"), 
                             cutoff.distance / max(distmat), color.divisions)

pheatmap(res$r, 
    main = "Correlation Matrix",
    annotation_col = Heat_test, 
    fontsize = 18,
    breaks = seq(-1,1, length.out=(color.divisions + 1)), 
    color = color, 
    show_rownames = F, 
    show_colnames = F, 
    legend = T)


---

