Subject of some previous questions - e.g. this. I cannot get the sf layer to render over the ggmap layer.
require(ggmap)
require(raster)
require(sf)
target_bbox <- c(left = -0.65, bottom = 51.2, right = 0.45, top = 51.8)
map = get_stamenmap(target_bbox, zoom = 9, maptype = 'toner-2010')
# can test this with: ggmap(map)
e = extent(list(x = c(-0.65, 0.45), y = c(51.2, 51.8)))
r = raster(volcano)
crs(r) = CRS("+proj=longlat +datum=WGS84")
p = raster::rasterToPolygons(r)
sf = st_as_sf(p) %>% st_transform(3857)
# test with: ggplot() + geom_sf(data = sf, aes(fill = layer), col = NA)
ggmap(map) +
geom_sf(data = sf, aes(fill = layer), col = NA, alpha = 0.4, inherit.aes = FALSE)
Coordinate system already present. Adding new coordinate system, which will replace the existing one.
Coordinate system clash but no errors, yet no layer is visible. Any suggestions much appreciated.
This github solution - extracting the bounding box from the ggmap object with custom function ggmap_bbox()
- only succeeds in adding another warning to that above:
Warning message:
In st_is_longlat(x) :
bounding box has potentially an invalid value range for longlat data