knitr::opts_chunk$set(message = FALSE)
knitr::opts_chunk$set(echo = TRUE)



library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.0.3
library(sf)
## Warning: package 'sf' was built under R version 4.0.3
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(rnaturalearth)
library(rgdal)
## Warning: package 'rgdal' was built under R version 4.0.3
## Loading required package: sp
## Warning: package 'sp' was built under R version 4.0.3
## rgdal: version: 1.5-23, (SVN revision 1121)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.2.1, released 2020/12/29
## Path to GDAL shared files: C:/Users/nmmasto42/Documents/R/win-library/4.0/rgdal/gdal
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ runtime: Rel. 7.2.1, January 1st, 2021, [PJ_VERSION: 721]
## Path to PROJ shared files: C:/Users/nmmasto42/Documents/R/win-library/4.0/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.4-5
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading rgdal.
## Overwritten PROJ_LIB was C:/Users/nmmasto42/Documents/R/win-library/4.0/rgdal/proj
library(ggspatial)
## Warning: package 'ggspatial' was built under R version 4.0.4
library(spData)
## Warning: package 'spData' was built under R version 4.0.4
## To access larger datasets in this package, install the spDataLarge
## package with: `install.packages('spDataLarge',
## repos='https://nowosad.github.io/drat/', type='source')`
library(cowplot)
## Warning: package 'cowplot' was built under R version 4.0.4

Download geodata and create custom bbox

#data-------------------------------------------------------------------

#need geo data
world <- ne_countries(scale = "medium", returnclass = "sf")
states <- map_data("state")
data("us_states", package = "spData")

#personal data file with xy coords and identifier (i.e., state)
#for some reason R projects is not working w/ markdown so full path needed
locs <- read.csv("C:/Users/nmmasto42/Documents/TennesseeTech/DISSERTATION/SPRING_MIGRATION/Analysis/mapping/data/traplocs.csv")

#create custom bounding box for ggplot map1
coords <- matrix(c(-96, 28,
                   -85, 28,
                   -85, 37.5,
                   -96, 37.5,
                   -96, 28),
                 ncol = 2, byrow = TRUE)
bbox <- Polygon(coords)
bbox <- SpatialPolygons(list(Polygons(list(bbox), ID = "a")), 
                        proj4string = CRS('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs'))

plot(bbox, axes = TRUE) #did it work?

bbox <- st_as_sf(bbox, plot = FALSE, fill = TRUE) #make sf object

U.S. map w bbox

Make the inset map first by combining us_states and bbox:

#plot1------------------------------------------------------------------------
ggplot1 <-ggplot() +
  geom_sf(data = us_states, fill = "white") +
  geom_sf(data = bbox, fill = NA, size = 1.2, col = "black") + 
  coord_sf(crs = "+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs")+
  theme_void()

#take a look
ggplot1

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Create main plot

#plot2------------------------------------------------------------------------

ggplot2 <- ggplot(data = world) +                                     #world
  geom_sf(fill = "gray92") +                                          #light gray
  geom_polygon(data = states, aes(x = long, y = lat, group = group),  #states outline
               color = "black", fill = NA)+ 
  geom_point(data = locs, aes(x = x, y = y, color = state),           #trap locs 
             alpha = 0.65, size = 3)+
  scale_color_manual(values = c("darkred", "gold3", "darkorchid"),    #custom colors
                     name = "Trapping Locations",
                     breaks = (c("ar", "la", "tn")),
                     labels = (c("Arkansas", "Louisiana", "Tennessee"))) +
  theme_bw() +
  xlab("Longitude") + ylab("Latitude") +                              #x and y axis
  annotate(geom = "text", x = -90, y = 28, label = "Gulf of Mexico",  #annotations
           fontface = "italic", color = "grey22", size = 5) +
  annotate(geom = "text", x = -92.8, y = 32, label = "LA", 
           fontface = "italic", color = "grey22", size = 5)+
  annotate(geom = "text", x = -92.8, y = 35, label = "AR", 
           fontface = "italic", color = "grey22", size = 5)+
  annotate(geom = "text", x = -88, y = 35.8, label = "TN", 
           fontface = "italic", color = "grey22", size = 5)+
  annotate(geom = "text", x = -89.8, y = 33, label = "MS", 
           fontface = "italic", color = "grey22", size = 5)+
  annotate(geom = "text", x = -92.8, y = 39, label = "MO", 
           fontface = "italic", color = "grey22", size = 5)+
  annotate(geom = "text", x = -87, y = 33, label = "AL", 
           fontface = "italic", color = "grey22", size = 5)+
  coord_sf(crs = "+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs")+                        #albers
  coord_sf(xlim = c(-95, -85), ylim = c(28, 37), expand = TRUE) +     #isolate study area
  annotation_north_arrow(location = "bl", which_north = "true",       #north arrow
                         pad_x = unit(0.5, "in"), pad_y = unit(0.2, "in"),
                         style = north_arrow_fancy_orienteering) +
  theme(legend.title = element_text(size = 13),                       #text sizes
        legend.text = element_text(size = 11),
        axis.title = element_text(size = 13),
        axis.text = element_text(size = 11),
        element_line(color = "black"))

ggplot2 #take a look

## Combine plots and save

#inset map----------------------------------------------------------------------
inset <- ggdraw() +
  draw_plot(ggplot2) +
  draw_plot(ggplot1, x = 0.58, y = 0.68, width = 0.3, height = 0.3) #size and location

inset #look

ggsave(filename = "C:/Users/nmmasto42/Documents/TennesseeTech/DISSERTATION/SPRING_MIGRATION/Analysis/mapping/plots/figure1.png",  #save
       plot = inset,
       dpi = 300)