I would like to as kfor any advice how to solve ggmap issue.
lets suppose we have some spatial model and residuals and then we would like to plot it in the map.
When using ggmap
function I can see baseline background plot and regend for base_layer - fill
, however it cannot be seen in the plot.
I provide reproduceble example:
library(ggmap)
library(maptools)
library(ggplot2)
#map background
bboxPrague <- c(14.22,49.94,14.71,50.18)
ggMapPrague <- get_map(location = bboxPrague, source = "stamen",maptype = "toner", crop = TRUE, zoom = 12)
ggmap(ggMapPrague)
d = data.frame(
pred_res = runif(2000, -50, 50),
lon = runif(2000, 49.94, 50.18),
lat = runif(2000, 14.22, 14.71)
)
d
#top&bottom coding and discreting pred_res....8
d$res_coded<-replace(d$pred_res,d$pred_res<(-1),8)
d$res_coded<-replace(d$res_coded,d$pred_res>=-1,7)
d$res_coded<-replace(d$res_coded,d$pred_res>=-0.4,6)
d$res_coded<-replace(d$res_coded,d$pred_res>=-0.1,5)
d$res_coded<-replace(d$res_coded,d$pred_res>=0,4)
d$res_coded<-replace(d$res_coded,d$pred_res>=0.1,3)
d$res_coded<-replace(d$res_coded,d$pred_res>=0.4,2)
d$res_coded<-replace(d$res_coded,d$pred_res>=1,1)
d %>% head
d$res_coded %>% head
d$res_coded = as.factor(d$res_coded)
ggmap(ggMapPrague, base_layer = ggplot(d, aes(x = lat, y = lon, fill = res_coded)),extent="device",legend = "topleft") +
geom_tile(alpha=0.5) +
scale_fill_brewer(palette="RdYlGn",name="Residual",labels = c("[1;+inf)","[0.4;1)","[0.1;0.4)","[0;0.1)","[-0.1;0)","[-0.4;-0.1)","[-1;-0.4)","(-inf;-1)"))