rggplot2plotmaps

How can I combine two maps in R?


I have provided the sample codes below. My issue involves placing 'map2' in a square at the top left corner of 'map1' and adding an arrow from 'map2' to a specific location on 'map1'. I've searched the site, but most commonly discussed topics are related to merging two data layers.

library (tidyverse)
  library (rnaturalearth)
  world <- rnaturalearth::ne_countries(scale = "medium", returnclass = "sf")

  map1<- ggplot(data = world) +
    geom_sf() +
    #annotation_scale(location = "bl", width_hint = 0.2) +
    #annotation_north_arrow(location = "tr", which_north = "true",
    #                     pad_x = unit(0.83, "in"), pad_y = unit(0.02, "in"),
    #                   style = north_arrow_fancy_orienteering) +
    coord_sf(xlim = c(35, 48), ylim=c(12, 22))+
    xlab("Longtitude")+
    ylab("Latitude")


  map2<- ggplot(data = world) +
    geom_sf() +
    #annotation_scale(location = "bl", width_hint = 0.2) +
    #annotation_north_arrow(location = "tr", which_north = "true",
    #                     pad_x = unit(0.83, "in"), pad_y = unit(0.02, "in"),
    #                   style = north_arrow_fancy_orienteering) +
    coord_sf(xlim = c(5, 45), ylim=c(5, 45))+
    xlab("Longtitude")+
    ylab("Latitude")

  map2

Solution

  • Just adding a cowplot option:

    final <-
      ggdraw(map1) +
      draw_plot(map2, x = 0.7, y = .63, width = .3, height = .3)+ 
      geom_segment(aes(x = 0.92, y = 0.75, xend = 0.5, yend = 0.4),    
                   arrow = arrow(length = unit(0.5, "cm"))) 
    
    
    
    

    enter image description here

    This is a bit annoying because you can't use the original coordinates as you could with patchwork, so it takes some trial and errow -- but I couldn't figure out a way with patchwork to have the arrow go from the inset to the overall map and be visible on both layers.


    Should note I edited the inset map to not have legends and have border:

    map2<- ggplot(data = world) +
      geom_sf() +
      #annotation_scale(location = "bl", width_hint = 0.2) +
      #annotation_north_arrow(location = "tr", which_north = "true", 
      #                     pad_x = unit(0.83, "in"), pad_y = unit(0.02, "in"),
      #                   style = north_arrow_fancy_orienteering) +
      coord_sf(xlim = c(5, 45), ylim=c(5, 45))+
    theme_void() + 
      theme(panel.border = element_rect(color = "black", linewidth=1, fill = NA))
    map2