library(ggplot2)
library(ggmap)
library(sf)
#> Linking to GEOS 3.6.2, GDAL 2.3.0, proj.4 5.1.0
nc <- st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
nc <- st_transform(nc, 4326)
bounding <- sf::st_bbox(nc) # get the bounding box of the data - we'll use this to get the basemap
names(bounding) <- c("left","bottom","right","top")
map <- get_map(location = bounding, zoom = 6)
# Just plotting, the geom_sf layer is not aligned properly:
ggmap(map) +
geom_sf(data = nc, aes(fill = AREA), inherit.aes = FALSE)
#> Coordinate system already present. Adding new coordinate system, which will replace the existing one.devtools::source_gist("1467691edbc1fd1f7fbbabd05957cbb5",
filename = "ggmap_sf.R")
# Plot with new function, add the sf object (must st_transform to 3857):
ggmap_sf(map) +
geom_sf(data = st_transform(nc, 3857), aes(fill = AREA), inherit.aes = FALSE)
#> Coordinate system already present. Adding new coordinate system, which will replace the existing one.
#> Coordinate system already present. Adding new coordinate system, which will replace the existing one.Created on 2018-06-15 by the reprex package (v0.2.0).

