27 Figure 6
27.1 Summary
This is the accessory documentation of Figure 6.
The Figure can be recreated by running the R script plot_F6.R
from a (bash
terminal):
cd $BASE_DIR
Rscript --vanilla R/fig/plot_F6.R \
\
2_analysis/summaries/fst_outliers_998.tsv \
2_analysis/geva/ 2_analysis/GxP/bySNP/
27.2 Details of plot_F6.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, BAMMtools and on the package hypoimg.
27.2.1 Config
The scripts start with a header that contains copy & paste templates to execute interactively or debug the script:
#!/usr/bin/env Rscript
# run from terminal:
# Rscript --vanilla R/fig/plot_F6.R \
# 2_analysis/summaries/fst_outliers_998.tsv \
# 2_analysis/geva/ \
# 2_analysis/GxP/bySNP/
# ===============================================================
# This script produces Figure 6 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_F6.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)
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_F6.R ──────────────────────────────────────────────
#> Parameters read:
#> ★ 1: 2_analysis/summaries/fst_outliers_998.tsv
#> ★ 2: 2_analysis/geva/
#> ★ 3: 2_analysis/GxP/bySNP/
#> ─────────────────────────────────────────── /current/working/directory ──
The directories for the different data types are received and stored in respective variables. Also, we set a few parameters for the plot layout:
# config -----------------------
<- as.character(args[1])
outlier_file <- as.character(args[2])
geva_path <- as.character(args[3]) gxp_path
27.2.2 Actual Script Start
<- read_tsv(outlier_file)
outlier_data
<- outlier_data[c(2, 13, 14),] %>%
data pmap_dfr(get_gxp_and_geva)
<- 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
<- 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("Snout", "Bars", "Peduncle"),
highlight_rects levels = c("Snout", "Bars", "Peduncle")),
gid_label = gid_label)
<- data %>%
p_done 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 ggplot() +
geom_rect(data = highlight_rects,
aes( xmin = 0, xmax = Inf,
ymin = 0, ymax = Inf),
color = rgb(.75,.75,.75),
size = .4,
fill = rgb(.9,.9,.9,.5))+
::geom_hypo_grob(inherit.aes = FALSE,
hypoimgdata = annotation_grobs_tib,
aes(grob = grob), x = .15, y = .78, angle = 0, width = .35, height =.35)+
geom_pointdensity(size = plot_size,
aes(x = PostMedian,y = p_wald))+
facet_grid(gid_label ~ trait, scales = "free_y")+
scale_x_log10(labels = scales::trans_format("log10", scales::math_format(10^.x)))+
scale_y_continuous(trans = reverselog_trans(10),
labels = scales::trans_format("log10", scales::math_format(10^.x)))+
scale_color_viridis_c("Density", option = "B")+
labs(y = "G x P *p* value <sub>Wald</sub>",
x = "Derived allele age (generations)")+
guides(color = guide_colorbar(barwidth = unit(120, "pt"),
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)
)
Finally, we can export Figure 6.
hypo_save(plot = p_done,
filename = "figures/F6.pdf",
width = f_width_half,
height = f_width_half,
comment = plot_comment,
device = cairo_pdf,
bg = "transparent")