I would like to use custom symbols and annotation on a geom_sf polygon layer that is placed on a google basemap. Initially, I had difficulty with the basemap and polygon lining up correctly
So I used this Stack Overflow solution to fix it. Now the basemap and polygon layer line up
But I would like custom symbols using geom_image and labels using annotate. The unmodified basemap and annotation layer with custom symbols works
Below I expanded andyteucher and user1453488's code and added the geom_image and annotate functions that I would like to use.
I tried the alternate solution by plotting with sf package, but I could not figure out how to use custom symbols on the plot with latitude and longitude. My actual data has latitude and longitude. I am open to an alternate solutions, even though I am more familiar with ggmap.
Do you know how to put a google base map, polygon layer with accurate projection, custom symbols (generated from a data frame with geographic coordinates) and labels on the same map? Thank you.
# code modified and expanded from andyteucher's solution
library(ggplot2)
library(ggmap)
library(sf)
library(ggimage)
#load shapefile with sf package
nc <- st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
# Transform nc to EPSG 3857 (Pseudo-Mercator, what Google uses)
nc_3857 <- st_transform(nc, 3857)
map <- get_map("north carolina", maptype = "satellite", zoom = 6, source = "google")
# display misaligned polygon with basemap
a <- unlist(attr(map,"bb")[1, ])
bb <- st_bbox(nc)
ggplot() +
annotation_raster(map, xmin = a[2], xmax = a[4], ymin = a[1], ymax = a[3]) +
xlim(c(bb[1], bb[3])) + ylim(c(bb[2], bb[4])) +
geom_sf(data = nc, aes(fill = AREA))
# Define a function to fix the bbox to be in EPSG:3857
ggmap_bbox <- function(map) {
if (!inherits(map, "ggmap")) stop("map must be a ggmap object")
# Extract the bounding box (in lat/lon) from the ggmap to a numeric vector,
# and set the names to what sf::st_bbox expects:
map_bbox <- setNames(unlist(attr(map, "bb")),
c("ymin", "xmin", "ymax", "xmax"))
# Coonvert the bbox to an sf polygon, transform it to 3857,
# and convert back to a bbox (convoluted, but it works)
bbox_3857 <- st_bbox(st_transform(st_as_sfc(st_bbox(map_bbox, crs = 4326)), 3857))
# Overwrite the bbox of the ggmap object with the transformed coordinates
attr(map, "bb")$ll.lat <- bbox_3857["ymin"]
attr(map, "bb")$ll.lon <- bbox_3857["xmin"]
attr(map, "bb")$ur.lat <- bbox_3857["ymax"]
attr(map, "bb")$ur.lon <- bbox_3857["xmax"]
map
}
# Use the function:
mapModified <- ggmap_bbox(map)
# display aligned polygon with basemap
ggmap(mapModified) +
coord_sf(crs = st_crs(3857)) + # force the ggplot2 map to be in 3857
geom_sf(data = nc_3857, aes(fill = AREA), inherit.aes = FALSE)
# create simple data frame for labels and symbols
name <- c("Point1", "Point2")
x <- c(-82, -78)
y <- c(35, 36)
symbol <- c("https://www.r-project.org/logo/Rlogo.png",
"https://jeroenooms.github.io/images/frink.png")
df <- data.frame(name, x, y, name, symbol)
# example symbols and label on basemap (but polygon layer misaligned)
ggplot() +
annotation_raster(map, xmin = a[2], xmax = a[4], ymin = a[1], ymax = a[3]) +
xlim(c(bb[1], bb[3])) + ylim(c(bb[2], bb[4])) +
geom_sf(data = nc, aes(fill = AREA)) +
annotate("text", -80, 36.5, label = "My text here") +
geom_image(aes(x, y, image = symbol))
# after the polygons are aligned the geom_image no longer displays
ggmap(mapModified) +
coord_sf(crs = st_crs(3857)) + # force the ggplot2 map to be in 3857
geom_sf(data = nc_3857, aes(fill = AREA), inherit.aes = FALSE) +
geom_image(aes(x, y, image = symbol))
# after the polygons are aligned the annotate is dropped from display
ggmap(mapModified) +
coord_sf(crs = st_crs(3857)) + # force the ggplot2 map to be in 3857
geom_sf(data = nc_3857, aes(fill = AREA), inherit.aes = FALSE) +
annotate("text", -80, 36.5, label = "My text here")