38 Supplementary Figure 11

38.1 Summary

This is the accessory documentation of Figure S11. The Figure can be recreated by running the R script plot_SF11.R:

cd $BASE_DIR

Rscript --vanilla R/fig/plot_SF11.R \
    2_analysis/newhyb/nh_input/NH.Results/

38.2 Details of plot_SF11.R

In the following, the individual steps of the R script are documented. It is an executable R script that depends on the accessory R package GenomicOriginsScripts, as well as on the packages hypoimg, hypogen and patchwork

38.2.1 Config

The scripts start with a header that contains copy & paste templates to execute or debug the script:

#!/usr/bin/env Rscript
# run from terminal:
# Rscript --vanilla R/fig/plot_SF11.R \
#     2_analysis/newhyb/nh_input/NH.Results/
# ===============================================================
# This script produces Suppl. Figure 11 of the study "Rapid radiation in a
# highly diverse marine environment" by Hench, Helmkampf, McMillan and Puebla
# ---------------------------------------------------------------
# ===============================================================
# args <- c("2_analysis/newhyb/nh_input/NH.Results/")
# script_name <- "R/fig/plot_SF11.R"
args <- commandArgs(trailingOnly = FALSE)

The next section processes the input from the command line. It stores the arguments in the vector args. The needed R packages are loaded and the script name and the current working directory are stored inside variables (script_name, plot_comment). This information will later be written into the meta data of the figure to help us tracing back the scripts that created the figures in the future.

Then we drop all the imported information besides the arguments following the script name and print the information to the terminal.

# setup -----------------------
renv::activate()
library(GenomicOriginsScripts)
library(prismatic)
library(paletteer)
library(patchwork)
library(ggtext)
library(hypoimg)
library(hypogen)

cat('\n')
script_name <- args[5] %>%
  str_remove(., '--file=')

plot_comment <- script_name %>%
  str_c('mother-script = ', getwd(), '/', .)

args <- process_input(script_name, args)
#> ── Script: R/fig/plot_SF11.R ────────────────────────────────────────────
#> Parameters read:
#> ★ 1: 2_analysis/newhyb/nh_input/NH.Results/
#> ────────────────────────────────────────── /current/working/directory ──

The directory containing the PCA data is received and stored in a variable. Also the default color scheme is updated and the size of the hamlet ann.

# config -----------------------
base_dir <- as.character(args[1])
# locate hybridization data files
folders <- dir(base_dir)
# load data and create plots by location
p_loc <- c("bel", "hon", "pan") %>%
  map(plot_loc)
# compose figure from the individual panels
p_done <- (p_loc[[1]] +  guides(fill = guide_legend(title = "Hybrid Class")) + theme_hyb(legend.position = c(1,1)) ) +
  (p_loc[[2]] + theme_hyb() ) +
  (p_loc[[3]] + theme_hyb() )  +
  plot_layout(ncol = 1, heights = c(10,15,3) %>% label_spacer())+
  plot_annotation(tag_levels = 'a')

Finally, we can export Figure S11.

# export the final figure
hypo_save(filename = "figures/SF11.pdf",
       plot = p_done,
       height = 16,
       width = 10,
       device = cairo_pdf,
       comment = plot_comment)