39 Supplementary Figure 12
39.1 Summary
This is the accessory documentation of Figure S12.
The Figure can be recreated by running the R script plot_SF12.R
:
cd $BASE_DIR
Rscript --vanilla R/fig/plot_SF12.R \
\
ressources/species_order_alpha.txt \
2_analysis/dstats/hyp_ld05_dtrios_BBAA.txt \
2_analysis/dstats/BBAA_ld05.csv 2_analysis/dstats/BBAA_sign_ld05.csv
39.2 Details of plot_SF12.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
39.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_SF12.R \
# ressources/species_order_alpha.txt \
# 2_analysis/dstats/hyp_ld05_dtrios_BBAA.txt \
# 2_analysis/dstats/BBAA_ld05.csv \
# 2_analysis/dstats/BBAA_sign_ld05.csv
# ===============================================================
# This script produces Suppl. Figure 12 of the study "Rapid radiation in a
# highly diverse marine environment" by Hench, Helmkampf, McMillan and Puebla
# ---------------------------------------------------------------
# ===============================================================
# args <- c("ressources/species_order_alpha.txt",
# "2_analysis/dstats/hyp_ld05_dtrios_BBAA.txt",
# "2_analysis/dstats/BBAA_ld05.csv",
# "2_analysis/dstats/BBAA_sign_ld05.csv")
# script_name <- "R/fig/plot_SF12.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(prismatic)
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_SF12.R ────────────────────────────────────────────
#> Parameters read:
#> ★ 1: ressources/species_order_alpha.txt
#> ★ 2: 2_analysis/dstats/hyp_ld05_dtrios_BBAA.txt
#> ★ 3: 2_analysis/dstats/BBAA_ld05.csv
#> ★ 4: 2_analysis/dstats/BBAA_sign_ld05.csv
#> ────────────────────────────────────────── /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])
species_order_file <- as.character(args[2])
trios_file <- as.character(args[3])
bbaa_file <- as.character(args[4]) signif_file
<- function(P2, P3, ...){
two_chr_to_sorted_pair <- as.numeric(factor(P2, levels = sorter$group))
n1 <- as.numeric(factor(P3, levels = sorter$group))
n2 <- if(n1 < n2){c(P2, P3)} else { c(P3, P2)}
char_sorted str_c(char_sorted[[1]], "-", char_sorted[[2]])
}
<- read_tsv(species_order_file, col_names = "group") sorter
<- 16
p_cap
<- read_tsv(bbaa_file) %>%
data filter(p_adjusted <= .05) %>%
mutate(pair = pmap_chr(.,two_chr_to_sorted_pair)) %>%
group_by(pair) %>%
mutate(p_is_min = p_adjusted == min(p_adjusted)) %>%
filter(p_is_min) %>%
mutate(d_is_max = Dstatistic == max(Dstatistic)) %>%
filter(d_is_max) %>%
ungroup() %>%
separate(pair, into = c("p_left", "p_right"), sep = "-", remove = FALSE) %>%
mutate(p_adjusted = if_else(p_adjusted < 10^-p_cap,10^-p_cap, p_adjusted ))
<- data %>%
data_prep ::select(p_left, p_right, Dstatistic, p_adjusted) %>%
dplyrbind_rows(data %>%
::select(p_left = p_right, p_right = p_left,
dplyr Dstatistic, p_adjusted))
<- cross_df(list(p_left = sorter$group ,
data_full p_right = sorter$group)) %>%
left_join(data_prep) %>%
mutate(p_left = factor(p_left, levels = sorter$group),
p_right = factor(p_right, levels = rev(sorter$group)))
<- read_tsv(signif_file) %>%
data_signif mutate(pair = pmap_chr(.,two_chr_to_sorted_pair)) %>%
separate(pair, into = c("p_left", "p_right"), sep = "-", remove = FALSE) %>%
mutate(p_left = factor(p_left, levels = sorter$group),
p_right = factor(p_right, levels = rev(sorter$group))) %>%
mutate(p_adjusted = if_else(p_adjusted < 10^-p_cap, 10^-p_cap, p_adjusted) )
<- scales::colour_ramp(colors = RColorBrewer::brewer.pal(5,"RdYlBu")[c(1,2,4,5)])((1:7)/7) %>% clr_saturate(.1)
clr
<- c(0, .01)
d_lim <- c(1, p_cap) p_lim
<- data_full %>%
p_done filter(p_left != "Outgroup" ) %>%
mutate(check1 = as.numeric(p_left),
check2 = as.numeric(p_right)) %>%
filter(check2 < 17 - check1) %>%
ggplot()+
geom_tile(aes(x = p_left,
y = p_right,
fill = Dstatistic,
color = after_scale(clr_darken(fill))) ) +
geom_point(data = data_signif %>% filter(p_value < .05),
aes(x = p_left,
y = p_right,
size = -log10(p_adjusted)),
shape = 1,
alpha = .4) +
scale_fill_gradientn(colours = rev(clr),
limits = d_lim,
na.value = rgb(1,1,1,.2)) +
scale_size(range = c(.1, 6),
limits = c(1,16),
breaks = c(1,8,16)) +
guides(fill = guide_colorbar(title = "D",
title.position = "top",
barwidth = unit(.6,"npc"),
barheight = unit(4,"pt"),
order = 1),
size = guide_legend(title = "-log<sub>10</sub> *( p<sub>adjusted</sub> )*",
order = 2,
title.position = "top")) +
coord_equal() +
theme_minimal(base_size = plot_text_size) +
theme(legend.position = c(.95,1),
legend.text.align = .5,legend.title.align = 1,
legend.justification = c(1,1),
legend.direction = "horizontal",
legend.box.just = "right",
panel.grid = element_blank(),
axis.text.x = element_text(angle = 90, vjust = .5),
axis.title = element_blank(),
legend.title = element_markdown())
Finally, we can export Figure S12.
::hypo_save(filename = "figures/SF12.pdf",
hypoimgwidth = f_width_half,
height = f_width_half,
device = cairo_pdf,
bg = "transparent",
comment = plot_comment)