Is It Possible To Fix The Scale In Ggplot?
1
1
Entering edit mode
11.1 years ago
k.nirmalraman ★ 1.1k

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 • 24k views
ADD COMMENT
1
Entering edit mode

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.

ADD REPLY
1
Entering edit mode
11.1 years ago

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))

ADD COMMENT

Login before adding your answer.

Traffic: 2029 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6