Home > Back-end >  Trying to create a map with a picture in the upper right corner in R using annotation_custom
Trying to create a map with a picture in the upper right corner in R using annotation_custom

Time:09-26

Does anyone know how I can insert an image in the upper right corner of a map in r? I have included an example of what I would like to do. If you are able to provide any guidance that would be great. Feel free to use an example of a map (does not need to be with the data provided)

Shapefile data is downloadable at enter image description here

Current Map

enter image description here

What I want it to look like

enter image description here

CodePudding user response:

I think the problem with your own code is simply that you need negative values to represent degrees West. Here's a fully reproducible example that should achieve what you want.

library(sf)
library(tidyverse)
library(ggsn)
library(jpeg)
library(grid)

shp <- st_read("C:/Users/Administrator/Downloads/USA_Detailed_Water_Bodies")

shp$OBJECTID <- as.factor(shp$OBJECTID)

shp_subset <- filter(shp, OBJECTID %in% c('245511'))

Plot <- ggplot(data = shp_subset)   
  geom_sf()   
  coord_sf()  
  theme_bw()   
  scalebar(data = shp_subset, dist = 5, dist_unit = "km", height = 0.01, 
           transform = TRUE, model = "WGS84", location = "bottomleft", 
           anchor = c(x = -94.7, y = 47.50), st.bottom = FALSE, st.size = 2.5, 
           st.dist = 0.015)

img <- readJPEG(readBin("https://i.stack.imgur.com/IqqPv.jpg", "raw", 1e6)) %>%
         rasterGrob()

Plot   
  annotation_custom(img, -94.4, -94.25, 47.4, 47.5)  
  annotation_custom(rectGrob(gp = gpar(fill = NA)), -94.4, -94.25, 47.4, 47.5)

Created on 2022-09-25 with reprex v2.0.2

  • Related