46 Supplementary Figure 19
46.1 Summary
This is the accessory documentation of Figure S19.
The Figure can be recreated by running the R script plot_SF19.R
:
cd $BASE_DIR
Rscript --vanilla R/fig/plot_SF19.R \
\
2_analysis/summaries/fst_outliers_998.tsv \
2_analysis/geva/ 2_analysis/GxP/bySNP/
46.2 Details of plot_SF19.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
46.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_SF19.R \
# 2_analysis/summaries/fst_outliers_998.tsv \
# 2_analysis/geva/ \
# 2_analysis/GxP/bySNP/
# ===============================================================
# This script produces Suppl. Figure 19 of the study "Rapid radiation in a
# highly diverse marine environment" by Hench, Helmkampf, McMillan and Puebla
# ---------------------------------------------------------------
# ===============================================================
# args <- c( "2_analysis/summaries/fst_outliers_998.tsv", "2_analysis/geva/", "2_analysis/GxP/bySNP/" )
# script_name <- "R/fig/plot_SF19.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)
library(ggpointdensity)
library(scales)
library(grid)
library(prismatic)
library(patchwork)
cat('\n')
<- args[5] %>%
script_name str_remove(.,'--file=')
<- script_name %>%
plot_comment str_c('mother-script = ',getwd(),'/',.)
::rule( left = str_c(crayon::bold('Script: '),crayon::red(script_name)))
cli= args[7:length(args)]
args cat(' ')
cat(str_c(crayon::green(cli::symbol$star),' ', 1:length(args),': ',crayon::green(args),'\n'))
::rule(right = getwd()) cli
#> ── Script: R/fig/plot_SF19.R ────────────────────────────────────────────
#> Parameters read:
#> ★ 1: 2_analysis/summaries/fst_outliers_998.tsv
#> ★ 2: 2_analysis/geva/
#> ★ 3: 2_analysis/GxP/bySNP/
#> ────────────────────────────────────────── /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])
outlier_file <- as.character(args[2])
geva_path <- as.character(args[3]) gxp_path
<- read_tsv(outlier_file)
outlier_data
<- outlier_data[1:3,] %>% pmap_dfr(get_gxp_and_geva)
data1 <- outlier_data[4:6,] %>% pmap_dfr(get_gxp_and_geva)
data2 <- outlier_data[7:9,] %>% pmap_dfr(get_gxp_and_geva)
data3 <- outlier_data[10:12,] %>% pmap_dfr(get_gxp_and_geva)
data4 <- outlier_data[13:15,] %>% pmap_dfr(get_gxp_and_geva)
data5 <- outlier_data[16:18,] %>% pmap_dfr(get_gxp_and_geva) data6
<- data1 %>%
data bind_rows(data2) %>%
bind_rows(data3) %>%
bind_rows(data4) %>%
bind_rows(data5) %>%
bind_rows(data6)
<- c(100, 10^6)
xrange <- rgb(1, 0.5, 0.16) color
<- 8
base_length <- .15
base_lwd <- "black" base_line_clr
<- tibble(intercept = 5000) splitage
<- outlier_data$gid
gid_label c(2, 13, 14)] <- c( LG04_1 = "LG04 (A)", LG12_3 = "LG12 (B)", LG12_4 = "LG12 (C)" ) gid_label[
<- c(Bars = "#79009f", Snout = "#E48A00", Peduncle = "#5B9E2D") %>%
gxp_clr darken(factor = .95) %>%
set_names(., nm = c("Bars", "Snout", "Peduncle"))
<- tibble(svg = hypo_trait_img$grob_circle[hypo_trait_img$trait %in% c( 'Snout', 'Bars', 'Peduncle')],
annotation_grobs layer = c(4,3,7),
color = gxp_clr[c(1,3,2)]) %>%
::pmap(.l = ., .f = hypo_recolor_svg) %>%
purrrset_names(nm = c( "LG12_3","LG12_4","LG04_1"))
$LG12_3 <- hypo_recolor_svg(annotation_grobs$LG12_3,
annotation_grobslayer = 7, color = gxp_clr[[1]] %>%
%>% clr_lighten(.25)) clr_desaturate
<- tibble(gid = names(annotation_grobs),
annotation_grobs_tib grob = annotation_grobs) %>%
mutate( gid_label = gid_label[gid],
trait = factor( c( "Bars", "Peduncle", "Snout"),
levels = c("Snout", "Bars", "Peduncle")))
<- tibble(trait = factor( c(NA, "Snout", rep(NA, 10), "Bars", "Peduncle", rep(NA, 4)),
highlight_rects levels = c("Snout", "Bars", "Peduncle")),
gid_label = gid_label)
<- data %>%
p_1 pivot_longer(names_to = "trait",
values_to = "p_wald",
cols = Bars:Snout) %>%
mutate(trait = factor(trait, levels = c("Snout", "Bars", "Peduncle")),
gid_label = gid_label[gid]) %>%
filter(Clock == "J",
== 1,
Filtered %in% outlier_data$gid[1:9],
gid !(gid %in% outlier_data$gid[2])) %>%
ggplot()
<- data %>%
p_2 pivot_longer(names_to = "trait",
values_to = "p_wald",
cols = Bars:Snout) %>%
mutate(trait = factor(trait, levels = c("Snout", "Bars", "Peduncle")),
gid_label = gid_label[gid]) %>%
filter(Clock == "J",
== 1,
Filtered %in% outlier_data$gid[10:18],
gid !(gid %in% outlier_data$gid[c(13:14)])) %>%
ggplot()
<- function(p){
complete_p +
p geom_pointdensity(size = plot_size,
aes(x = PostMedian,y = p_wald)) +
facet_grid(gid ~ trait, scales = "free_y"
+
) scale_x_log10(labels = scales::trans_format("log10", scales::math_format(10^.x))) +
scale_y_continuous(trans = reverselog_trans(10), #limits = c(10^0, 10^-90),
labels = scales::trans_format("log10", scales::math_format(10^.x))) +
scale_color_viridis_c("Density", option = "B", limits = c(0,750)#, limits = c(0,1100)
+
) labs(y = "G x P *p* value <sub>Wald</sub>",
x = "Derived allele age (generations)") +
guides(color = guide_colorbar(title.position = "top",
barwidth = unit(.4, "npc"),
barheight = unit(3, "pt"))) +
theme_minimal() +
theme(text = element_text(size = plot_text_size),
axis.title.y = element_markdown(),
legend.position = "bottom",
plot.subtitle = element_markdown(),
axis.line = element_line(colour = base_line_clr,
size = base_lwd),
strip.background = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major = element_line(size = plot_lwd))
}
<- cowplot::plot_grid( complete_p(p_1) +
p_done theme(legend.position = "none"),
complete_p(p_2) + theme(legend.box.margin = unit(c(7,0,7,0),"pt")))
Finally, we can export Figure S19.
hypo_save(plot = p_done,
filename = "figures/SF19.pdf",
width = f_width,
height = f_width,
comment = plot_comment,
device = cairo_pdf,
bg = "transparent")