In addition to creating plots, R could also be used to create maps for analysis and data presentation. In this example, I will be using R to create a map of Saint Lucia. I will also be adding the locations of the fish landing sites to the map and inserting Saint Lucia’s EEZ boundary.
This section of code is used to load the necessary libraries for the creation of the maps
library(tidyverse)
library(ggplot2) #library used for making graphics (barcharts, line graphs, etc.)
library(sf)#library used for reading spatial data (used for creating maps)
library(terra)
library(tidyterra)
library(metR)
library(nngeo)
library(ggspatial)
library(geofacet)
This section of code is used to load the data files that will be used for creating the map
Website to get shapefile for your country’s EEZ (https://www.marineregions.org/gazetteer.php?p=details&id=8416)
Website to get shapefile for your country. You can use either (https://data.humdata.org/dataset) or (https://gadm.org/)
#TO READ IN THE DATA FOR COUNTRY OUTLINE, USE EITHER (1):
read_gadm <- function(country_code = "LCA") {
pth <- paste0("https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/gadm41_",
country_code,
".gpkg")
suppressWarnings(read_sf(pth))}
saintlucia <- read_gadm(country_code = "LCA")
# #OR (2)
# saintlucia <- read_sf("path to the folder that contains the shapefile for your country")
#READING IN FILE WITH SAINT LUCIA'S EEZ
slu_eez <- read_sf("../data_raw/eez/eez.shp") #change path to the folder where your country's eez shapefile is stored
This map shows the different landing sites on the island
#The source() function makes it possible to retrieve information that is stored in separate scripts
source("../R/gps.R", local = knitr::knit_global()) #gps.R is used to store the coordinates of the primary and secondary landing sites in Saint Lucia
#This code is used to create a map of SLU that shows the landing sites on island
map_slu <- ggplot()+
geom_sf(data = saintlucia, colour = "black") + #loading Saint Lucia's coastline as the first layer
geom_point(data = psites, aes(x = lon, y = lat ),colour = "red") + #inserting primary landing sites as points
geom_text (data= psites, #geom_text is used to amend the text on the map. You could also use geom_text_repel
aes(x= lon,
y= lat,
label= site), size = 3, #size controls the size of the point
nudge_x=c(0.035,-0.02,-0.022,0.025,0.025,-0.03,-0.025,-0.024,-0.025,-0.03),#use nudge to shift the names to the left (-) or to the right. nudge_y moves it up or down
nudge_y=c(-0.012,-0.007,0,0,0,0.005,-0.005,0,-0.005,0),
family = "Times New Roman", #one can also change the text style used
fontface = "bold") +
geom_point(data = ssites, aes(x = lon, y = lat ),colour = "black") + #inserting secondary landing sites as points
geom_text(data= ssites,
aes(x= lon,
y= lat,
label= site),size=3,
nudge_x=c(-0.02,-0.02,0.025,-0.02),
family = "Times New Roman") +
theme_bw()+ #to get rid of grey background
xlab("Longitude") +
ylab("Latitude") +
coord_sf(xlim=c(-61.2,-60.8)) #change the limits of the map to include the community names
map_slu
#Plotting Saint Lucia's EEZ boundary
ggplot()+
geom_sf(data = saintlucia) +
geom_sf(data = slu_eez, colour = "black") +
theme_bw() #to get rid of grey background
The following code shows different ways that you could save the plots and maps that have been created. Below are three (3) ways one could save maps in R
#Method One (1)- save the map directly to the desired path
ggsave("../figs/map_slu.jpg", #replace path with the path to the folder that you would like to save your map in
width = 20, height= 20, units = "cm")
#Method Two (2)- assign the map to a name (in this case map_slu, and save the map with a new name and your specific requirements and in your desired path.)
ggsave("saintlucia.jpg", #replace path with the folder that you would like to save your map in
plot = map_slu,
device = "jpeg",
width = 20, #Note: if no dimensions are given, R would save the map/figure using most suitable dimensions
height = 20,
units = "cm",
path = "../figs")
#Method Three (3)- You do not need to assign the code for a map/plot to a specific name. R will save the last map/plot that was created using last_plot() function
ggsave("saintlucia_eez.jpg",
plot = last_plot(),#Note: Be mindful that whatever plot that was created last, would be the plot that will be saved.
device = "jpeg",
width = 20,
height = 20,
units = "cm",
path = "../figs") #replace path with the folder that you would like to save your map in
This code shows one possible way to create bathymetry maps (One can obtain bathymetry information from https://download.gebco.net). Code created by EinarHJ
z <- rast("../data_raw/gebco_2023_n15.0_s13.0_w-62.0_e-60.0.tif")
names(z) <- "depth"
bb <- st_bbox(c(xmin = -61.25, xmax = -60.6,
ymin = 13.4, ymax = 14.3))
eez1 <-
slu_eez |>
nngeo::st_remove_holes() |>
st_cast("LINESTRING") |>
st_crop(bb)
z <- crop(z, bb)
i <- values(z) > 0
values(z)[i] <- NA
i <- values(z) < -1500
values(z)[i] <- -1500
# Hillshade with grey colors
slope <- terrain(z, "slope", unit = "radians")
aspect <- terrain(z, "aspect", unit = "radians")
hill <- shade(slope, aspect, 10, 340)
p <-
ggplot() +
theme_void() +
geom_spatraster(data = hill, show.legend = FALSE) +
# Note the scale, grey colours
scale_fill_gradientn(colours = grey(0:100 / 100), na.value = NA) +
ggnewscale::new_scale_fill() +
geom_spatraster(data = z, alpha = 0.55, show.legend = FALSE) +
geom_spatraster_contour(data = z, colour = "red", linewidth = 0.4,
breaks = c(-25, -50, -100, -200, -500, -750, -1000)) +
scale_fill_viridis_c() +
geom_sf(data = eez1, colour = "white") +
theme(legend.position = "none") +
annotation_scale(pad_x = unit(1, "cm"),
pad_y = unit(1, "cm"))
p