knitr::opts_chunk$set(echo = TRUE)
This vignette demonstrates how to create IDW maps using akgfmaps. If you haven't installed akgfmaps already, follow the installation instructions on the \code{README}. Then, load the akgfmaps package.
library(akgfmaps)
The package and all of the dependencies should load. If they do not, make sure you are running R >= 3.6, and verify that all of the dependencies listed in the \code{DESCRIPTION} file are installed.
Function documentation is accessible using the standard R \code{?} convention (e.g. documentation for \code{make_idw_map} can be accessed using \code{?make_idw_map}).
?make_idw_map
Start by loading and inspecting the 2017 EBS and NBS yellowfin sole CPUE data that is included with the package.
# Load 2017 EBS and NBS yellowfin sole data yfs2017 <- akgfmaps::YFS2017 head(yfs2017)
The data includes the species common name, year, station id, latitude, longitude, and catch-per-unit effort in kilograms per hectare. At minimum, the data must include latitude, longitude, and catch-per-unit effort data to plot CPUE.
The function for performing the IDW and making a plot is \code{make_idw_map}. The function uses gstat to perform the IDW, then raster, stars, and sf for plotting. As arguments to the \code{make_idw_map} function, Users can specify:
See function documentation for default arguments.
Below, we call the make_idw_map argument, specifying: * region = "bs.all" (northern + southern EBS), which has a predefined extrapolation area. * \code{grid.cell = c(0.02,0.02)
opt1 <- make_idw_map(x = yfs2017, # Pass data as a data frame region = "bs.all", # Predefined bs.all area set.breaks = "jenks", # Gets Jenks breaks from classint::classIntervals() in.crs = "+proj=longlat", # Set input coordinate reference system out.crs = "EPSG:3338", # Set output coordinate reference system grid.cell = c(20000, 20000), # 20x20km grid key.title = "yellowfin sole") # Include yellowfin sole in the legend title
The \code{make_idw_map} function returns a list containing object which contain information about the region, number of breaks, and key.title, which are used by other functions to manipulate plots and add labels.
opt1$region opt1$n.breaks opt1$key.title
The extrapolation.grid is a stars object that contains discrete CPUE values.
class(opt1$extrapolation.grid)
The continuous.grid is a stars object that contains continuous CPUE values.
class(opt1$continuous.grid)
The last object in the list is a ggplot object.
opt1$plot
Map layers are included in the package and do not need to be provided separately. Changes to text sizing, color palette, and addition of plot labels is done using change_text_size, change_fill_color, and add_idw
The \code{change_fill_color} function changes the fill color palette using predefined color palettes. Refer to the function documentation for a list of colors. Below, I show two transformations, one to grey scale and one to red, using pipe operators \code{|>} and passing the new fill color to the \code{new.scheme} argument. If no argument is provided to new.scheme, a color palette selector tool will open in a separate window \code{colorspace::choose_palette()}. The predefined color palettes are colorblind and grayscale friendly.
# Grey Note that the WHOLE list that was returned by make_idw_map should be passed to the function. opt1.grey <- opt1 |> change_fill_color(new.scheme = "grey", show.plot = FALSE) # Red opt1.red <- opt1 |> change_fill_color(new.scheme = "red", show.plot = FALSE) opt1.grey$plot opt1.red$plot
The returned list objects retain the same structure as the objects from make_idw_map
Labels can be added by passing a data frame of labels, locations, and label types to the \code{add_idw_labels} function. Alternatively, pre-defined labels can be added to the plot based on the region. Below, pre-defined labels are added to the plot using \code{geom_text} and \code{geom_shadowtext}. Below, labels and a scale bar are added to the greyscale plot using pre-defined settings based on the plot region.
opt1.grey <- opt1.grey |> add_map_labels() opt1.grey$plot
Custom labels can also be appended to the object, with sizing and orientation based on defined 'types.' In the example below, a data frame is passed with plotting locations. Internally, the \code{add_idw_plot_labels} function transforms long/lat coordinates in eastings to the Albers Equal Area projection of the map. See function documentation for options to turn on/off spatial transformations.
# ONLY custom label opt1.red2 <- opt1.red |> add_map_labels(new.places = data.frame(type = "islands", region = "bs.all", x = -163, y = 57, lab = "Bristol Bay"), lab.replace = TRUE) opt1.red2$plot
# Add custom label opt1.red <- opt1.red |> add_map_labels(new.places = data.frame(type = "islands", region = "bs.all", x = -163, y = 57, lab = "Bristol Bay"), lab.replace = FALSE) opt1.red$plot
After labels and colors are added to the map, the \code{create_map_file()} function can be used to create publication-quality png files. It automatically rescales axis and text labels for some pre-defined plot sizes. Output file names can be specified using file.prefix, or they will be extracted automatically from the input list. The function passes plotting arguments to \code{png()}. The argument \code{try.change_text_size} indicates whether the function should try to resize text based on a pre-defined scaling factor.
opt1.grey |> create_map_file(width = 8, # Passed to png() height = 6, # Passed to png() units = "in", # Passed to png() res = 300, # Passed to png() bg = "transparent", # Passed to png() file.path = "./", try.change_text_size = TRUE)
The \code{change_text_size()} function can be used resize plot text prior to creating a map if the desired size for the map is not pre-defined.
opt.grey2 <- opt1.grey |> change_text_size(size.mult = 0.5) opt.grey2$plot
See the breaks vignette for details.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.