library(data.table) library(ggplot2) library(colorspace) library(patchwork) # cut wt fun source("R/fun_wt_vals.R")
paths = list.files("data/ssf_fits", full.names = T) data = lapply(paths, fread) data = rbindlist(data) data = data[growth %in% c(0.01, 0.02, 0.03)]
wt_data = fread("data/data_lt_wts_scaled.csv") wt_data[, move_strategy := dplyr::case_when( wt_4 > 0.6 ~ "prey tracking", ((wt_4 > 0) & (wt_3 > 0) & (abs(wt_4 - wt_3) < 0.1)) ~ "prey and handler tracking", wt_3 > 0.5 ~ "handler tracking", wt_3 < -0.5 ~ "handler avoiding", wt_2 > 0.5 ~ "non-handler tracking", wt_2 < -0.5 ~ "non-handler avoiding", T ~ "mixed" )] wt_data[, move_strategy := factor( move_strategy, levels = c( "prey tracking", "handler tracking", "prey and handler tracking", "non-handler avoiding", "handler avoiding", "mixed" ) ) ]
# select only significant predictors data = data[p.value <= 0.05, !(c("std.error", "statistic"))] data = data[, !("p.value")] data[, term := dplyr::case_when( term == "cell_r" ~ "ssf_R" )] data = dcast(data, id + growth + type + rep_ ~ term, value.var = "estimate") data = merge(data, wt_data, by.x = c("id", "growth", "rep_", "type"), by.y = c("id", "growth", "rep_", "type"), all.x = T, all.y = F) data = data[id < 100,] data = split(data, by = "type") data = data[c("exploit", "obligate")]
plot_ssf_hist = lapply(data, function(df) { ggplot(df)+ geom_hline( yintercept = 0, lty = 2, col = 2 )+ ggdist::stat_histinterval( aes( factor(growth), ssf_R, fill = comp_strat ), col = "grey20", slab_colour = "black", slab_size = 0.2, # binwidth = 0.2, alpha = 0.7, position = position_nudge(x = 0.0), point_colour = NA )+ scale_fill_manual( values = c( "forager" = "darkseagreen", "klept" = "brown" ), labels = c( "forager" = "Forager", "klept" = "Kleptoparasite" ) )+ scale_x_discrete( limits = rev(c("0.01", "0.02", "0.03")) )+ scale_y_continuous( breaks = c(0, 1, 3, 5, 7, 9), limits = c(-2, 10) )+ coord_flip( expand = F, xlim = c(0.8, NA) )+ theme_test( base_size = 8 )+ theme( axis.title.x = ggtext::element_markdown(), axis.title.y = ggtext::element_markdown(), axis.text.y = element_text( angle = 90, hjust = 0.5 ), axis.text.x = element_text(size = 6), legend.position = "top", legend.title = element_text(face = "bold"), legend.key.height = unit(2, "mm") )+ labs( y = "Estimated relative selection for <i>r<i>", x = "Landscape productivity *r<sub>max</sub>*", fill = "Competitive strategy", colour = "Movement strategy" )+ guides( fill = guide_legend(ncol = 2, title.position = "top"), colour = guide_legend(ncol = 2, title.position = "top", override.aes = list(size = 2)) ) }) plot_ssf_hist = wrap_plots( plot_ssf_hist, guides = "collect" )+ plot_layout( tag_level = "new" )
# filter data for main strategies data = rbindlist(data) data_type_summary = data[ , .N, by = c("growth", "move_strategy", "rep_", "comp_strat", "type") ] data_type_summary = data_type_summary[N > 10,] data = merge(data, data_type_summary) data = split(data, by = "type") # now plot plot_ssf_box = lapply(data, function(df) { ggplot(df)+ geom_hline( yintercept = 0, lty = 2, col = 2 )+ ggdist::stat_histinterval( aes( factor(growth), ssf_R, fill = move_strategy ), col = "grey20", slab_colour = "black", slab_size = 0.2, # binwidth = 0.2, alpha = 0.7, position = position_nudge(x = 0.0), point_colour = NA )+ scale_fill_manual( values = c( "prey tracking" = "seagreen", "prey and handler tracking" = "royalblue", "non-handler avoiding" = "red", "handler avoiding" = "orange", "handler tracking" = "lightblue", "mixed" = "lightgrey", "NA" = "transparent" ), breaks = c( "prey tracking", "handler tracking", "prey and handler tracking", "non-handler avoiding", "handler avoiding", "mixed" ) )+ scale_y_continuous( breaks = c(0, 1, 3, 5, 7, 9), limits = c(-2, 10) )+ scale_x_discrete( limits = rev(c("0.01", "0.02", "0.03")) )+ coord_flip( expand = F, xlim = c(0.8, NA) )+ theme_test( base_size = 8 )+ theme( axis.title.x = ggtext::element_markdown(), axis.title.y = ggtext::element_markdown(), axis.text.y = element_text( angle = 90, hjust = 0.5 ), axis.text.x = element_text(size = 6), legend.position = "top", legend.title = element_text(face = "bold"), legend.key.height = unit(2, "mm") )+ labs( y = "Estimated relative selection for <i>r<i>", x = "Landscape productivity *r<sub>max</sub>*", fill = "Movement strategy" )+ guides( fill = guide_legend(ncol = 3, title.position = "top"), colour = guide_legend(ncol = 3, title.position = "top", override.aes = list(size = 2)) ) }) plot_ssf_box = wrap_plots( plot_ssf_box, guides = "collect" )+ plot_layout( tag_level = "new" ) & theme( legend.position = "top" )
plot_ssf = wrap_plots( plot_ssf_hist, plot_ssf_box, ncol = 1 )& plot_annotation( tag_levels = c("A", 1) )& theme( plot.tag = element_text( face = "bold", size = 8 ), legend.position = "bottom" ) ggsave( plot_ssf, filename = "figures/fig_04.png", width = 120, height = 120, units = "mm" )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.