0

I am generating multiple heatmaps to be plotted as an animation. I want to reuse the color keys for different heatmaps to show the change with time. Currently the key is being reset for each heatmap.

Heatmap1: (https://i.stack.imgur.com/AT0mT.png) Heatmap2: (https://i.stack.imgur.com/iyiHm.png)

I would like the "total range" to be the same in both heatmaps. So Heatmap1 above should have a net "lighter" color.

The code I have at present is as follows:

mat  <- matrix(unlist(row), ncol=4, byrow=TRUE)
matm <- melt (mat)
p    <- ggplot (data=matm) +
        geom_tile (aes(x=X1, y=X2, fill=value), color="white")  +
        scale_fill_gradient (low="steelblue1", high="steelblue4")

row has data for each of the 16 cells. I convert it to a matrix, and then plot the same. But different rows have different ranges, and the range is reset each time.

I have tried the following modifications:

[1] Add breaks to the scale_fill

scale_fill_gradient (low="steelblue1", high="steelblue4",breaks=seq(3,6.4,by=0.05))

[2] Cut the data into factors and add breaks to the scale_fill

row_cut <- cut (as.numeric(row), breaks=seq(3,6.4,by=0.05), right=FALSE)
mat     <- matrix(unlist(row_cut), ncol=4, byrow=TRUE)
matm    <- melt (mat)
p       <- ggplot (data=matm) +
           geom_tile (aes(x=X1, y=X2, fill=value), color="white")  +
           scale_fill_brewer(palette = "Blues","intensity", breaks=seq(3,6.4,by=0.05))

But neither of the modifications helps.

(Aside: When I add breaks to scale_fill the legend is missing in the plots)

  • I think you should `?scales::rescale` your data anyway, but that would also help with your legend problem – rawr May 01 '14 at 22:18
  • @rawr To clarify, `rescale` does not fix my problem right? Because each heatmap will still have a different range of values. And I want to indicate that change in my eventual animation. –  May 01 '14 at 22:39
  • it rescales to a specified max and min. correlations, for example, range from -1 to 1; default is 0 to 1, so the minimum value in your data would be 0 and the max as 1 and all other values rescaled accordingly. If you don't want to do that, you could also see `?discrete_scale` and change the `limits` to be the same for both scales – rawr May 01 '14 at 23:42

1 Answers1

0

Use the limits parameter:

scale_fill_gradient(..., limits=c(3, 6.4))
BrodieG
  • 51,669
  • 9
  • 93
  • 146