This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Is It Possible To Fix The Scale In Ggplot?

Hi,

I am generating a few heatmaps using ggplot2 using the following code.

 ggplot(zscore.m, aes(variable, factor(EXT,levels=unique(EXT)))) + geom_tile(aes(fill = value,position="fill"),colour = "white") +scale_fill_gradientn(colours=c("white","orange","red","dark red"))

The scale is determined based on the values in the data (here zscore.m). Lets say the scale in one of the heatmap ranges from -5 to 5 and the other heatmap ranges from -8 to 8. The colour scale is adjusted accordingly. What I would like is to have is same colours for similar values, so both the heatmaps can be compared visually w/o having to refer to the scale time and again.

Thanks in advance for your help!

heatmap r

It's not clear if you want the same colour for the same value across plots, or similar colours for similar quantiles. If the former, format your all your data into a long dataframe and facet accordingly when plotting, the scale will be shared by default.

1 answer

You will need to make 2 modifications. Let's suppose that you want your scales to go from -4 to 4:

  1. Change all data values less than -4 or greater than 4 so that they are equal to 4 (otherwise they will be colored grey)
  2. Add "limits" to your scale_fill command:

    ... + scale_fill_gradientn(colours=c("white","orange","red","dark red"), limits=c(-4,4))

Log in to answer this question.