I am constructing two ion concentration maps in the UK using ggplot2
. The dataset used for mapping are lzn.kriged1
and lzn.kriged2
which basically has the same structure:
> str(lzn.kriged1)
Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
..@ data :'data.frame': 9962 obs. of 2 variables:
.. ..$ var1.pred: num [1:9962] 9.63 10.47 10.39 10.29 10.56 ...
.. ..$ var1.var : num [1:9962] 10.59 2.16 2.1 2.11 4.79 ...
..@ coords.nrs : int [1:2] 1 2
..@ coords : num [1:9962, 1:2] -6.35 -5.25 -5.19 -5.13 -5.72 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:9962] "1" "2" "3" "4" ...
.. .. ..$ : chr [1:2] "x1" "x2"
..@ bbox : num [1:2, 1:2] -8.1 49.95 1.73 60.82
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:2] "x1" "x2"
.. .. ..$ : chr [1:2] "min" "max"
..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
.. .. ..@ projargs: chr NA
I change the fill colour based on var1.pred
with the following condition:
below 10 = color1
>10-20 = color2
>20-30 = color3
above 30 = color4
I use cut
function to apply that condition:
lzn.kriged1 %>% as.data.frame %>% #lzn.kriged2 for MAP2
ggplot(aes(x=x1, y=x2)) +
geom_tile(aes(fill=cut(var1.pred,breaks=c(0,10,20,30,Inf),
labels = c("Below 10", ">10-20", ">20-30", "Above 30")))) +
coord_map() + theme(legend.title=element_blank())
which resulted in these maps:
In MAP 2, the maximum value of var1.pred
is 19. That is why the legend only shows two levels.
My questions are:
1) How can I control the fill color for each level? For example:
below 10 = blue
>10-20 = green
>20-30 = yelow
above 30 = red
2) How can I make those two maps to have the same colors and the same legend?