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)"))
In addition to set
base_layer = ggplot(aes(...), ...), one still needs to specify a geom (geometric object) layer, such asgeom_point(aes(...)),geom_rect(aes(...))orgeom_polygon(aes(...))appending to theggmap(...), in order to have data used in base_layer mapped to visual effects.For example,
We can also remove the background grey square in legend keys for a better visual (IMO), by appending with:
Note: in your example,
latandlonshould be swapped -- we usually havex = lon, y = lat. See the fixes in the following demo code.Demo
Issue with
geom_tilegeom_tiledraws rectangles specified by(center_x, center_y, width, height), whenwidthandheightare unspecified, they will be calculated as the minimum gap between two adjacent points, that is,width = min(abs(diff(df$x)))andheight = min(abs(diff(df$y))). In your case, thewidth = min(abs(diff(d$lon))) ~ 0.0001525275andheight = min(abs(diff(d$lat))) ~ 3.292719e-05(due torunif, actual value may vary a bit).This kind of automation will cause problem given the example or GPS coordinates represents "raw points" rather than "center of each region", because the gap between two adjacent "raw points" can be too narrow to worth a pixel to draw. In this case, we will have to manually specify the
widthandheightand bear in mind that the tiles will overlap.Consider your example, with:
To further illustrate the automated width and height of
geom_tile, consider the following simple example:Therefore, it will be just simpler to use
geom_pointto visualize "raw GPS points".