Search code examples
rggplot2ggmapcolor-palette

keep colour palette constant between plots


I need to compare two maps of the same quantities, I would like to keep the colour palette constant in the two graphs, for easing comprehension, but it looks like I don't get how to do so.

Should I set limits (e.g. the minimum between all the plots assigned to low and the highest level to high?)

Is there an easy way to do so?

I am new to this, so sorry if the solution is banal, I went through a lot of blog posts but looks like I am not finding anything.

My code:

fin<-get_map("Helsinki",zoom=12)
    ggmap(fin, legend="bottom")+
        geom_polygon(data=a,aes(x=a$long,y=a$lat, id=id, fill=Test_statistics), alpha=0.1, colour="white")

To give you an idea, this is an image

animage

and this is another

another

it is not clear at all! Images still need a bit of "prettyfying" it is just to give an idea

Basically what I would like is in this question, but for discrete (factor) values


Solution

  • I can't reproduce your plots because you've not given us the data, but setting limits in a scale_colour_gradient should work. See:

    http://docs.ggplot2.org/0.9.3.1/scale_gradient.html

    under "Tweak scale limits" (second example) where Hadley says:

    Setting the limits manually is also useful when producing multiple plots that need to be comparable

    For example (and I'm using points here for simplicity - you probably have to use scale_fill_gradient to set the fill colour for polygons - I don't have the time to build some polygons):

    > set.seed(310366); d=data.frame(x=runif(20),y=runif(20),
      z1=rnorm(20), z2=rnorm(20)+5)
    

    note that z1 has a range of about -1 to 1, and z2 has a range of 4 to 7. This helps us see the effect.

    > ggplot(d,aes(x=x,y=y,col=z1))+geom_point(size=8) +
        scale_colour_gradient(limit=range(c(d$z1,d$z2))
    
    > ggplot(d,aes(x=x,y=y,col=z2))+geom_point(size=8) + 
        scale_colour_gradient(limit=range(c(d$z1,d$z2)))
    

    produces two plots with the same limits on the palette legend, but the first one has very dark points because the values are all low (-1 to 1) and the second one has mostly light colours because the values are all high (4 to 7).

    Both sets of points have been coloured using the same mapping of value to colour because of the limit argument in the scale_colour_gradient function. You are mapping the fill attribute so I think you need scale_fill_gradient.