How to get multiple ggplot2 scale_fill_gradientn with same scale?

前端 未结 2 1558
死守一世寂寞
死守一世寂寞 2020-12-03 05:56
library(ggplot2)
library(cumplyr)
library(scales)
library(RColorBrewer)


myPalette <- colorRampPalette(rev(brewer.pal(11, \"Spectral\")))

x   = 1:5
y   = 1:5
pt         


        
相关标签:
2条回答
  • 2020-12-03 06:33

    In scale_fill_gradientn set the limits to be the same, so for example:

    scale_fill_gradientn(colours = myPalette(4),limits=c(0,4))
    

    And this is p1 and p2:

    p1

    p2

    0 讨论(0)
  • 2020-12-03 06:34

    Use limit in scale_gradientn:

    p1 <- ggplot(as.data.frame(d1))
    p1 <- p1 + geom_tile(aes(x = x, y = y, fill = val))
    
    p2 <- ggplot(as.data.frame(d2), aes(x = x, y = y, fill = val))
    p2 <- p2 + geom_tile(aes(x = x, y = y, fill = val)) 
    library(gridExtra)
    
    p1 <- p1 + scale_fill_gradientn(colours = myPalette(4), limits=c(0,4))
    p2 <- p2 + scale_fill_gradientn(colours = myPalette(4), limits=c(0,4))
    grid.arrange(p1, p2)
    

    enter image description here

    The grid.arrange stuff is just to avoid me having to copy/paste two pictures.

    0 讨论(0)
提交回复
热议问题