0

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

Community
  • 1
  • 1
Irene
  • 744
  • 1
  • 12
  • 36
  • 1
    Can you supply us with `a` or something equivalent to make an example we can cut/paste and use as basis for answers? – Spacedman Jun 26 '14 at 06:50
  • is it clear now? I have some problems in easing up these data, but the problem should be clear even without them. Hope so, at least. – Irene Jun 26 '14 at 07:09
  • If you compute the range of the data in all the datasets you want to map and put that in a limits parameter to a scale function. See my answer. – Spacedman Jun 26 '14 at 07:36
  • How is this for discrete (factor) values? Your maps show continuous colour scales from a numerical variable! This is why we like reproducible examples! – Spacedman Jun 26 '14 at 14:32

2 Answers2

2

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.

Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • This morning I thought you were completely right, but looking at the data again I have a doubt. I am afraid setting 'limits' is only not plotting values below the min and above the max, as stated in the document you shared, as a matter of fact even using scale_fill_gradient the colour palettes are not the same, since they singularly adapt to the single graphic "span". Am I wrong? If not is my question clear now? – Irene Jun 26 '14 at 13:45
  • 1
    Edited a bit with example. – Spacedman Jun 26 '14 at 14:26
  • I was wrong, you were right, thank you a lot for your help. I really appreciate it. really clear and adamant now :) – Irene Jul 03 '14 at 15:47
0

I didnt get your problem exctly, but try adding this to all your plots. Then the colour code should be uniform.

+scale_colour_brewer(pallette="Set1")

You can add any of the pallette's shown here with examples http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/#color-charts

Koundy
  • 5,265
  • 3
  • 24
  • 37
  • 1
    I think the OP wants an exact match of numbers to colours over different columns in the data frame on different maps. ggplot will stretch the palette to the range of the data. See the zlim arg in the base image function for a way to do it with the image function. `scale_colour_manual` might be the way to go with ggplot... – Spacedman Jun 26 '14 at 07:06
  • @Spacedman you are probably right, I am going through it, did not know of its existence. I'll get back to you, thanks for the moment. – Irene Jun 26 '14 at 07:12