34 Supplementary Figure 7
34.1 Summary
This is the accessory documentation of Figure S7.
The Figure can be recreated by running the R script plot_SF7.R
:
cd $BASE_DIR
run from terminal:
Rscript --vanilla R/fig/plot_SF7.R \
2_analysis/dxy/50k/
34.2 Details of plot_SF7.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
34.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_SF7.R \
# 2_analysis/dxy/50k/
# ===============================================================
# This script produces Suppl. Figure 7 of the study "Rapid radiation in a
# highly diverse marine environment" by Hench, Helmkampf, McMillan and Puebla
# ---------------------------------------------------------------
# ===============================================================
# args <- c('2_analysis/dxy/50k/')
# script_name <- "R/fig/plot_SF7.R"
<- commandArgs(trailingOnly = FALSE) args
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 -----------------------
::activate()
renvlibrary(GenomicOriginsScripts)
library(hypoimg)
library(hypogen)
library(ggtext)
cat('\n')
<- args[5] %>%
script_name str_remove(.,'--file=')
<- script_name %>%
plot_comment str_c('mother-script = ',getwd(),'/',.)
<- process_input(script_name, args) args
#> ── Script: R/fig/plot_SF7.R ────────────────────────────────────────────
#> Parameters read:
#> ★ 1: 2_analysis/dxy/50k/
#> ────────────────────────────────────────── /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 -----------------------
<- as.character(args[1]) dxy_path
# locate dxy data files
<- dir(dxy_path) files
# load dxy data
<- str_c(dxy_path,files) %>%
data ::map(get_dxy) %>%
purrrbind_rows() %>%
::set_names(., nm = c('scaffold', 'start', 'end', 'mid', 'sites', 'pi_pop1',
purrr'pi_pop2', 'dxy', 'fst', 'GSTART', 'gpos', 'run'))
# create table for the indication of genome wide average dxy in the plot background
# (rescale covered dxy range to the extent of the genome)
<- data %>%
global_bar # filter to non-overlaping windows only
filter( start %% 50000 == 1) %>%
select(sites, dxy, run) %>%
group_by(run) %>%
summarise(genome_wide_dxy = sum(sites*dxy)/sum(sites)) %>%
arrange(genome_wide_dxy) %>%
ungroup() %>%
mutate(run = fct_reorder(.f = run, .x = genome_wide_dxy),
scaled_dxy = genome_wide_dxy/max(genome_wide_dxy))
# prepare plot annotaton images
<- global_bar %>%
grob_tibble mutate(loc = str_sub(run,4,6),
right = str_sub(run,1,3),
left = str_sub(run,8,10)) %>%
select(1,4:6) %>%
pmap(.,plot_pair_run) %>%
bind_rows()
# prepare plotting elements --------
# pre-define secondary x-axis breaks
<- scales::cbreaks(c(0,max(global_bar$genome_wide_dxy)),
sc_ax ::pretty_breaks(4)) scales
# pre-define secondary x-axis labels
<- str_c(c("", sc_ax$breaks[2:5]*1000),
labels c("0", rep("\u00B710^-3",4)))
# sort pair-wise population comparisons by average genome wide dxy
<- data %>%
data mutate(run = factor(run, levels = levels(global_bar$run)))
# compose final figure
<- ggplot()+
p_done # general plot structure separated by run
facet_wrap( .~run, as.table = TRUE, ncol = 1, dir = 'v')+
# add genome wide average dxy in the background
geom_rect(data = global_bar %>% mutate(xmax = scaled_dxy * hypo_karyotype$GEND[24]),
aes(xmin = 0, xmax = xmax, ymin = -Inf, ymax = Inf), color = rgb(1,1,1,0),fill = clr_below)+
# add LG borders
geom_vline(data = hypogen::hypo_karyotype, aes(xintercept = GEND), color = hypo_clr_lg)+
# add dxy data points
geom_point(data = data, aes(x = gpos, y = dxy),
size=.2,color = plot_clr) +
# add fish images
geom_hypo_grob2(data = grob_tibble,
aes(grob = grob, rel_x = .945, rel_y = .5),
angle = 0, height = .9, width = .13)+
# axis layout
scale_x_hypo_LG(sec.axis = sec_axis(~ ./hypo_karyotype$GEND[24],
breaks = (sc_ax$breaks/max(global_bar$genome_wide_dxy))[1:5],
labels = labels,
name = expression(Genomic~position/~Genome~wide~italic(d[XY]))))+
scale_y_continuous(name = expression(italic(d[XY])), breaks = c(0,.01, .02))+
# set plot extent
coord_cartesian(xlim = c(0, hypo_karyotype$GEND[24]*1.135))+
# general plot layout
theme_hypo()+
theme(strip.text = element_blank(),
legend.position = 'none',
axis.title.x = element_text(),
axis.text.x.bottom = element_markdown(colour = 'darkgray'))
Finally, we can export Figure S7.
# export final figure
hypo_save(filename = 'figures/SF7.png',
plot = p_done,
width = 8,
height = 12,
dpi = 600,
type = "cairo",
comment = plot_comment)
system("convert figures/SF7.png figures/SF7.pdf")
system("rm figures/SF7.png")
<- str_c("exiftool -overwrite_original -Description=\"", plot_comment, "\" figures/SF7.pdf")
create_metadata system(create_metadata)